Golang Log日志库

本文介绍了Golang的log包,提供了一个简单的日志实现,包括Logger类型和方便使用的Print、Fatal、Panic系列方法。这些方法将消息输出到标准错误,并在每个日志条目上显示日期和时间。日志库支持自定义输出目标,以及Printf、Print、Println、Fatal、Fatalf、Fatalln、Panic、Panicf和Panicln等函数。
摘要由CSDN通过智能技术生成
参考

Package log implements a simple logging package. It defines a type, Logger, with methods for formatting output. It also has a predefined ‘standard’ Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and Panic[f|ln], which are easier to use than creating a Logger manually. That logger writes to standard error and prints the date and time of each logged message. Every log message is output on a separate line: if the message being printed does not end in a newline, the logger will add one. The Fatal functions call os.Exit(1) after writing the log message. The Panic functions call panic after writing the log message.

log包是一个简单的日志,定义了type,Logger和一些输出格式。可以跟Print,Fatal,Panic一起使用,使用Fatal,Panic的时候会在打出日志信息后调用本身。可以很简单的手动创建一个Logger。每跳日志信息是单独的一行。

例子1
package main

import (
	"bytes"
	"fmt"
	"log"
)

func main() {
   
	var (
		buf    bytes.Buffer
		logger = log.New(&buf, "logger: ", log.Lshortfile)
	)

	logger.Print("Hello, log file!")

	fmt.Print(&buf)
}

输出结果为:

logger: prog.go:15: Hello, log file!
例子2
package main

import (
	"bytes"
	"fmt"
	"log"
)

func main() {
   
	var (
		buf    bytes.Buffer
		logger = log.New(&buf, "INFO: ", log.Lshortfile)

		infof = func(info 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值