glog
is a universal high-performance log management module that provides powerful and easy-to-use log management functions and is one of the core components of the GoFrame
development framework.
Introduction
Usage:
import "github.com/gogf/gf/v2/os/glog"
API Documentation:
https://pkg.go.dev/github.com/gogf/gf/v2/os/glog
Brief Description:
- The
glog
module has a fixed log file name format*.log
, using.log
as the fixed log file name suffix. glog
supports file output, log levels, log categorization, debug management, call tracing, chaining operations, rotation splitting, and many more rich features.- You can use the
glog.New
method to createglog.Logger
objects for custom log printing, but it is recommended to use the default package methods provided byglog
to print logs. - When using package methods to modify module configuration, note that any
glog.Set*
setting methods will take effect globally. - The default time format for log content is
time [level] content newline
, wheretime
is accurate to the millisecond level,level
is optionally output,content
is the parameter input of the calling side, andnewline
is optionally output (some methods automatically add a newline character to the log content). Example log content:2018-10-10 12:00:01.568 [ERRO] An error occurred
. Print*/Debug*/Info*
methods output log content to the standard output (stdout
), and to prevent the mixing of logs,Notice*/Warning*/Error*/Critical*/Panic*/Fatal*
methods also output log content to the standard output (stdout
).- The
Panic*
methods will trigger apanic
error method after outputting log information. - The
Fatal*
methods will stop the process running after outputting log information and return a process status code of1
(the normal program exit status code is0
).
Features
The glog
component has the following notable features:
- Easy to use, powerful functionality
- Supports configuration management with a unified configuration component
- Supports log levels
- Supports color printing
- Supports chaining operations
- Supports specifying output log files/directories
- Supports tracing
- Supports asynchronous output
- Supports stack printing
- Supports debug information switch
- Supports custom
Writer
output interface - Supports custom log
Handler
processing - Supports custom log
CtxKeys
keys - Supports
JSON
format printing - Supports
Flags
feature - Supports
Rotate
rotation splitting feature
Singleton Object
The log component supports the singleton pattern, using g.Log(singleton name)
to obtain different singleton log management objects. The purpose of providing singleton objects is to allow different log management objects to be used for different business scenarios. It is recommended to use the g.Log()
method to obtain singleton objects for logging operations. This method will automatically read the configuration file and initialize the singleton object, and this initialization operation will only be executed once.
Differences between glog.Print
and g.Log().Print
glog
is the package name of the log component, andg.Log()
is a singleton object of a log component.- The
g.Log()
singleton object is maintained through the object management componentg
package. During object initialization, it will automatically read the log configuration. It is easy to use, and in most cases, it is recommended to use this method to use the log component. glog
is used in the form of an independent component, and by default, it directly outputs logs to the terminal. You can also set global configuration through configuration management methods or create independent log objects usingNew
.
User: Why are there two ways to print logs? Which method should I use?
Answer:
Since each component of the framework is decoupled, the framework can be used as an independent tool library. For example, if the project only needs to use the log component, you can directly use the methods of the glog
package, without introducing other components. However, in project engineering use, the use of the tool library may be more cumbersome, often involving configuration and component initialization (in most cases, this will lead to secondary encapsulation). Therefore, the framework also provides a coupled package called the g
package. This package loads some commonly used components by default. g.Log()
is one of the log components, which will automatically read and initialize the configuration object according to engineering specifications, quickly initialize the log object and achieve singleton management, greatly simplifying the use of logs under engineering. For more introductions, please refer to: Objects
Documents
📄️ Logging - Configuration
Configuration management functionality of the log component in the GoFrame framework, including how to manage Logger objects through configuration files and configuration methods. The log component supports multiple configuration formats, and its modular design allows independent log output configuration. Configuration items cover log paths, output levels, and terminal displays, with log levels supporting multiple modes to ensure flexible recording of information at each level.
📄️ Logging - Log Level
Using the log component in the GoFrame framework to manage and set log levels, including specific usage examples of SetLevel, SetLevelStr, and SetLevelPrint methods. We also discuss how to filter and display log content through different log level names for flexible log management.
📄️ Logging - File Directory
Use the logging component in the GoFrame framework to set the name and directory path of log files. Through the SetFile method, users can customize the format of log files and support gtime time format. Through the SetPath method, users can write log content to a specified directory and use gfpool to optimize file writing efficiency.
📄️ Logging - Chaining Operations
The glog module in the GoFrame framework supports chaining operations for logging functionality. This includes setting log output paths, log file categorization, log levels, enabling trace printing, and more. Additionally, it provides examples of setting file backtrack values and implementing asynchronous log output, optimizing the logging experience comprehensively.
📄️ Logging - Color Printing
Using the log component's color printing feature in the GoFrame framework to enhance log visibility. By adding font colors to highlight different log levels, including Debug, Info, Notice, Warning, Error, etc. Additionally, this article provides examples of enabling log color printing in configuration files and code, and explains the default color settings for different log levels.
📄️ Logging - Context
Usage of glog log component in the GoFrame framework, particularly how to achieve log printing through Context variables. The article provides a detailed explanation on configuring and using custom CtxKeys, and offers functionalities supporting trace. Additionally, it covers the implementation of the log Handler to help developers better integrate logging features.
📄️ Logging - Handler
This article introduces the new customizable log processing Handler feature of the glog component in the GoFrame framework starting from version v2.0. Developers can implement custom log output through the Handler, such as converting logs to Json format or outputting them to third-party services like Graylog. Detailed examples demonstrate how to use Handler for log processing in the GoFrame framework.
📄️ Logging - JSON Format
Using the glog component in the GoFrame framework to output logs in JSON format, suitable for parsing by log analysis tools. You will learn how to implement JSON log format output through map or struct parameters, and achieve more complex JSON content output in conjunction with the gjson.MustEncode method.
📄️ Logging - Asynchronous Output
Use the GoFrame framework for asynchronous log output to improve logging efficiency. You can set asynchronous output using SetAsync or chaining methods. Asynchronous output can reduce resource usage, but be aware of potential log disorder issues.
📄️ Logging - Stack Printing
The stack printing feature in the GoFrame framework log component allows developers to automatically print the stack information of logging method invocations. This feature can be triggered by various error log output methods like Notice*/Warning*/Error*/Critical*/Panic*/Fatal*, or obtained/printed via GetStack/PrintStack. This stack information is very useful for debugging error log information, especially in handling complex applications. This article helps developers better understand and apply the stack printing features of the log component through code examples.
📄️ Logging - Debug Info
The way to record debug information using Debug/Debugf methods in GoFrame framework, suitable for development and testing environments. Demonstrates how to control the output of debug information using SetDebug method with code examples, as well as how to disable debug logs via command line parameters and environment variables.
📄️ Logging - Writer Interface
Use the Writer interface of the glog module in the GoFrame framework to customize log output. By implementing a custom Writer object, logs can be flexibly output to different targets such as files, standard output, and Graylog. Additionally, sample code is provided to demonstrate how to implement log HOOK functionality to promptly notify monitoring services of serious errors.
📄️ Logging - Flags
Flags feature of the log component in the GoFrame framework, explaining how to control additional log output features through different constant combinations, including asynchronous output, call line number information printing, and various time format selections. These features enable developers to achieve more flexible logging and debugging.
📄️ Logging - Rotatation
Log rolling rotation feature of the glog component in GoFrame framework, including setting log file names to output by date, rolling rotation with RotateSize and RotateExpire, support for log file compression and backup, configuration examples, considerations, and more. Provides detailed configuration options and example code to help developers better manage log files.
📄️ Logging - FAQ
When using the GoFrame framework for logging, print the stack information of the error variable instead of the stack when the log method is called. Provides specific Go code examples to help developers record and debug error logs more accurately.