ZhangZhihui's Blog  

Examples of log levels from high to low are:
• Fatal
• Error
• Warn
• Info
• Debug

To set up log levels for your logs, you can add the level to each line of the log. The most straightforward way of doing this is to use the SetPrefix function:

    log.SetPrefix("INFO  ")
    log.Println("Some  event  happened")

If you call the SetPrefix function with the log level as the prefix, you will set the log level at the beginning of the line:

INFO 2022/01/26 00:48:15 Some event happened

Of course, the problem is that each time you want to add a log line with a different log level from the previous line, you need to call SetPrefix again. That is not a feasible solution.

Another method is to create new loggers upfront, with each logger representing a single log level:

import (
    "log"
    "os"
)

var (
    info  *log.Logger
    debug *log.Logger
)

func init() {
    info = log.New(os.Stderr, "INFO\t", log.LstdFlags)
    debug = log.New(os.Stderr, "DEBUG\t", log.LstdFlags)
}

To do this, you use the New function, which returns a logger, but you can also set the prefix and the fields to add to the log.

All you need to do to log events with different log levels is to use the appropriate loggers:

    info.Println("Some  informational  event  happened")
    debug.Println("Some  debugging  event  happened")

This is what will appear on the screen:

INFO	 2022/01/26  00:53:03  Some  informational  event  happened
DEBUG	 2022/01/26  00:53:03  Some  debugging  event  happened

You can also turn off logging for specific log levels to reduce the logfile size. For example, when you’re developing the program you can log debug events, but once you run in production, you no longer want to do that. A common practice is to use environment variables to indicate if you are running in development or production.

Retrieving the environment variable in Go is simple. You can use the os.Getenv function, passing it the environment variable name, and you will get the environment variable value. Using the same previous example, let’s set the ENV environment variable to production :

$  export  ENV=production

If you run this code, you will see that the debug event is not printed:

    info.Println("Some  informational  event  happened")
    if os.Getenv("ENV") != "production" {
        debug.Println("Some  debugging  event  happened")
    }

If you switch to the development environment, the debug event is printed again.

At the start of this recipe, you learned that one of the reasons to use log levels is to prioritize and filter off certain log levels. You can do this easily with a Unix - based system using the grep command.
Say you have a file named logfile.log with the following entries:
INFO 2023/01/06 00:21:32 Some informational event happened

DEBUG 2023/01/06 00:21:32 Some debugging event happened
INFO 2023/01/06 00:21:35 Another informational event happened
WARN 2023/01/06 00:23:35 A warning event happened
WARN 2023/01/06 00:33:11 Another warning event happened
ERROR 2023/01/06 00:33:11 An error event happened

You want to look at all error events first so you can use grep to filter out only error events:

$ grep "^ERROR" logfile.log

What if you want to see all log events except for debug events? You can just exclude the debug events using the v flag in grep :

$ grep -v "^DEBUG" logfile.log

Using grep is only the beginning. grep is a powerful tool, but there are many other log analysis tools you can use.

posted on 2023-09-29 17:55  ZhangZhihuiAAA  阅读(24)  评论(0编辑  收藏  举报