当前位置:网站首页>Use of go log package log
Use of go log package log
2022-06-27 23:24:00 【Cloud idle】
1、log brief introduction
golang Built in log package , Implement a simple log service . By calling log Function of the package , Simple log printing function can be realized .
2、log Use
log There is... In the bag 3 A series of log printing functions , , respectively, print series 、panic series 、fatal series .
Function series effect
print Print log only
panic Print log , Throw out panic abnormal
fatal Print log , Force end procedure (os.Exit(1)),defer Function does not execute
2.1 A simple example
func main() {
defer fmt.Println(“panic Processing before exiting ”)
log.Println(“println journal ”)
log.Panic(“panic journal ”)
log.Fatal(“ Program exit log ”)
}
Result example ( The actual result is not like this , because panic,fatal Will affect the execution of the program ):
2020/06/02 11:04:17 println journal
2020/06/02 11:04:17 panic journal
2020/06/02 11:04:17 panic Processing before exiting
2020/06/02 11:04:17 Program exit log
3、log To configure
3.1 standard log To configure
By default log Only the time will be printed , But in practice, we may also need to get the file name , Line number and other information ,log The package provides us with customized interfaces .
log The package provides two standards log Configuration related methods :
func Flags() int // Return to standard log Output configuration
func SetFlags(flag int) // Set standards log Output configuration
flag Parameters
const (
// Control details of output log information , Unable to control the order and format of the output .
// Output logs are separated by a colon after each entry : for example 2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
Ldate = 1 << iota // date :2009/01/23
Ltime // Time :01:23:23
Lmicroseconds // Microsecond level time :01:23:23.123123( For enhancement Ltime position )
Llongfile // File full pathname + Line number : /a/b/c/d.go:23
Lshortfile // file name + Line number :d.go:23( Will overwrite Llongfile)
LUTC // Use UTC Time
LstdFlags = Ldate | Ltime // standard logger The initial value of the
)
Example of standard log configuration
func init() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
func main() {
log.Println(“println journal ”)
}
Output results :
image.png
3.2 Log prefix configuration
log Package provides two related functions for log prefix configuration :
func Prefix() string // Returns the prefix configuration of the log
func SetPrefix(prefix string) // Set the log prefix
Log prefix configuration instance
func init() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
log.SetPrefix(“success”)
}
func main() {
log.Println(“println journal ”)
}
Output results :
image.png
3.3 Log output location configuration
The previous introduction is to output logs to the console ,golang Of log The package also supports exporting logs to a file .log The package provided. func SetOutput(w io.Writer) function , Output the log to a file .
Log output location configuration
func init() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
logFile, err := os.OpenFile(“./c.log”, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
log.Panic(“ Opening the log file is abnormal ”)
}
log.SetOutput(logFile)
}
func main() {
log.Println(“println journal ”)
}
result : Log output to the current directory c.log In file
image.png
4、 Customize logger
log Package provides us with built-in functions , Let us customize logger. In terms of effect , Is to put the title 3 Standard log configuration in 、 Log prefix configuration 、 The log output location configuration is integrated into one function , Make the log configuration less cumbersome .
log The package provides func New(out io.Writer, prefix string, flag int) *Logger Function to implement custom logger.
Example
var logger *log.Logger
func init() {
logFile, err := os.OpenFile(“./c.log”, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
log.Panic(“ Opening the log file is abnormal ”)
}
logger = log.New(logFile, “success”, log.Ldate | log.Ltime | log.Lshortfile)
}
func main() {
logger.Println(“ Customize logger”)
}
边栏推荐
- Aggregation and index optimization of mongodb basic operations
- Personal tree ALV template - accelerate your development
- 打造南沙“强芯”,南沙首届IC Nansha大会召开
- 因美纳陷数据泄露“丑闻”:我国基因数据安全能交给美企吗?
- Brief introduction to game phone platform
- MySQL十八:写语句的执行过程
- 【经典干货书】数据科学中的信息理论方法,561页pdf
- 官宣!Apache Doris 从 Apache 孵化器毕业,正式成为 Apache 顶级项目!
- 跟着存档教程动手学RNAseq分析(一)
- 华为伙伴暨开发者大会2022 | 麒麟软件携手华为共建计算产业,共创数智未来
猜你喜欢
随机推荐
【数字IC/FPGA】检测最后一个匹配序列的位置
【微服务|Sentinel】sentinel数据持久化
通过 MQTT 检测对象和传输图像
Feign通过自定义注解实现路径的转义
Azure Kinect DK 实现三维重建 (jetson实时版)
Azure Kinect DK realizes 3D reconstruction (PC non real time version)
This year's examinees are more "desperate" than the college entrance examination
【经典干货书】数据科学中的信息理论方法,561页pdf
[electron] 基础学习
「R」 Using ggpolar to draw survival association network diagram
跟着存档教程动手学RNAseq分析(四):使用DESeq2进行DE分析的QC方法
Small chip chiplet Technology
Practice torch FX: pytorch based model optimization quantization artifact
Discuz small fish game wind shadow legend business gbk+utf8 version template /dz game website template
seata
Technical implementation process of easycvr platform routing log function [code attached]
[electron] 基础学习
跟着存档教程动手学RNAseq分析(二)
Getting started with pytorch
跟着存档教程动手学RNAseq分析(三):使用DESeq2进行计数标准化









