当前位置:网站首页>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”)
}
边栏推荐
- MapReduce初级编程实践
- vivado VIO IP的用法
- Avoid using 100vh[easy to understand] at mobile terminal
- [electron] basic learning
- Livox Lidar+海康Camera 基于loam的实时三维重建生成RGB彩色点云
- How vivado adds timing constraints
- Design of STM32 and rc522 simple bus card system
- 【蓝桥杯集训100题】scratch数字计算 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第16题
- 【剑指Offer】48. 最长不含重复字符的子字符串
- Introduction to quantitative trading
猜你喜欢

How to use RPA to achieve automatic customer acquisition?

本机部署一个MongoDB单节点服务器,并启用auth验证、开启oplog

First principles (optimal solution theory)

实践torch.fx:基于Pytorch的模型优化量化神器

Ice cream or snow "high"?

Usage of vivado vio IP

The most illusory richest man in China is even more illusory

How vivado adds timing constraints

使用SQL进行数据去重的N种方法

EasyCVR平台路由日志功能的技术实现过程【附代码】
随机推荐
This year's examinees are more "desperate" than the college entrance examination
Introduction to quantitative trading
本机部署一个MongoDB单节点服务器,并启用auth验证、开启oplog
跨系统数据一致性问题解决方案汇总
通过 MQTT 检测对象和传输图像
捷码赋能案例:湖南天辰产研实力迅速提升!实战玩转智慧楼宇/工地等项目
Ice cream or snow "high"?
go日志包 log的使用
[js]var, let, const
First principles (optimal solution theory)
Fsnotify interface of go language to monitor file modification
未能加载文件或程序集“CefSharp.Core.Runtime.dll”或它的某一个依赖项。 不是有效的 Win32 应用程序。 (异常来自 HRESULT:0x800700C1)
Hiplot 在線繪圖工具的本地運行/開發庫開源
跟着存档教程动手学RNAseq分析(三):使用DESeq2进行计数标准化
pytorch 入门指南
NDSS 2022 接收的列表
MySQL删除表后如何使ID从1开始
To build a "strong core" in Nansha, the first IC Nansha conference was held in Nansha
最新云开发微信余额充电器特效小程序源码
Livox lidar+ Haikang camera generates color point cloud in real time