Skip to main content
Version: 2.8.x(Latest)

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:

  1. The glog module has a fixed log file name format *.log, using .log as the fixed log file name suffix.
  2. glog supports file output, log levels, log categorization, debug management, call tracing, chaining operations, rotation splitting, and many more rich features.
  3. You can use the glog.New method to create glog.Logger objects for custom log printing, but it is recommended to use the default package methods provided by glog to print logs.
  4. When using package methods to modify module configuration, note that any glog.Set* setting methods will take effect globally.
  5. The default time format for log content is time [level] content newline, where time is accurate to the millisecond level, level is optionally output, content is the parameter input of the calling side, and newline 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.
  6. 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).
  7. The Panic* methods will trigger a panic error method after outputting log information.
  8. The Fatal* methods will stop the process running after outputting log information and return a process status code of 1 (the normal program exit status code is 0).

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, and g.Log() is a singleton object of a log component.
  • The g.Log() singleton object is maintained through the object management component g 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 using New.
tip

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 - 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.