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

Log Level

Log levels are used to manage the output of logs, allowing us to enable or disable specific log content by setting particular levels. The log level can be set using two methods:

func (l *Logger) SetLevel(level int)
func (l *Logger) SetLevelStr(levelStr string) error
func (l *Logger) SetLevelPrint(enabled bool)

SetLevel Method

The SetLevel method allows you to set the log level, and the glog module supports the following log level constants:

LEVEL_ALL
LEVEL_DEV
LEVEL_PROD
LEVEL_DEBU
LEVEL_INFO
LEVEL_NOTI
LEVEL_WARN
LEVEL_ERRO

You can combine these levels using bitwise operations, for example, LEVEL_ALL is equivalent to LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT. You can also filter out LEVEL_DEBU/LEVEL_INFO/LEVEL_NOTI log content with LEVEL_ALL & ^LEVEL_DEBU & ^LEVEL_INFO & ^LEVEL_NOTI.

warning

There are other levels in the log module, such as CRIT/PANI/FATA, but these levels represent very serious errors and cannot be masked by the developer in the log level. For example, during serious errors, PANI/FATA error levels will trigger additional system actions: panic/exit.

Example of use:

package main

import (
"context"

"github.com/gogf/gf/v2/os/glog"
)

func main() {
ctx := context.TODO()
l := glog.New()
l.Info(ctx, "info1")
l.SetLevel(glog.LEVEL_ALL ^ glog.LEVEL_INFO)
l.Info(ctx, "info2")
}

After execution, the output result is:

2021-12-31 11:16:57.272 [INFO] info1

SetLevelStr Method

In most scenarios, you can set the log level using strings with the SetLevelStr method. The level configuration item in the configuration file is also set using strings. The supported log level strings, which are case-insensitive, are as follows:

"ALL":      LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
"DEV": LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
"DEVELOP": LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
"PROD": LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
"PRODUCT": LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
"DEBU": LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
"DEBUG": LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
"INFO": LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
"NOTI": LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
"NOTICE": LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
"WARN": LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
"WARNING": LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT,
"ERRO": LEVEL_ERRO | LEVEL_CRIT,
"ERROR": LEVEL_ERRO | LEVEL_CRIT,
"CRIT": LEVEL_CRIT,
"CRITICAL": LEVEL_CRIT,

You can see that the log levels set by the level names are filtered according to their severity: DEBU < INFO < NOTI < WARN < ERRO < CRIT. It also supports common deployment mode configuration names such as ALL, DEV, PROD.

Example of use:

package main

import (
"context"

"github.com/gogf/gf/v2/os/glog"
)

func main() {
ctx := context.TODO()
l := glog.New()
l.Info(ctx, "info1")
l.SetLevelStr("notice")
l.Info(ctx, "info2")
}

After execution, the output result is:

2021-12-31 11:20:15.019 [INFO] info1

SetLevelPrint Method

This method controls whether the default log output prints the log level indicator. By default, it prints the log level indicator.

Example of use:

package main

import (
"context"

"github.com/gogf/gf/v2/os/glog"
)

func main() {
ctx := context.TODO()
l := glog.New()
l.Info(ctx, "info1")
l.SetLevelPrint(false)
l.Info(ctx, "info2")
}

After execution, the output result is:

2023-03-14 10:28:18.598 [INFO] info1
2023-03-14 10:28:18.631 info1

Level Name

In logs, you will see content with various log levels printed in front, each with different level names. The default log level names are:

LEVEL_DEBU: "DEBU",
LEVEL_INFO: "INFO",
LEVEL_NOTI: "NOTI",
LEVEL_WARN: "WARN",
LEVEL_ERRO: "ERRO",
LEVEL_CRIT: "CRIT",
LEVEL_PANI: "PANI",
LEVEL_FATA: "FATA",

To maintain a unified log format and ensure an elegant layout, the names of log levels use the first four characters of the English words. If you have special requirements to change the log level names, you can set them using the following methods:

func (l *Logger) SetLevelPrefix(level int, prefix string)
func (l *Logger) SetLevelPrefixes(prefixes map[int]string)

Example of use:

package main

import (
"context"

"github.com/gogf/gf/v2/os/glog"
)

func main() {
ctx := context.TODO()
l := glog.New()
l.SetLevelPrefix(glog.LEVEL_DEBU, "debug")
l.Debug(ctx, "test")
}

After execution, the console output is:

2021-12-31 11:21:45.754 [debug] test