Categorygithub.com/admpub/log
modulepackage
1.4.0
Repository: https://github.com/admpub/log.git
Documentation: pkg.go.dev

# README

log

GoDoc Build Status Coverage Go Report

Other languages

简体中文 Русский

Description

log is a Go package providing enhanced logging support for Go programs. It has the following features:

  • High performance through asynchronous logging;
  • Recording message severity levels;
  • Recording message categories;
  • Recording message call stacks;
  • Filtering via severity levels and categories;
  • Customizable message format;
  • Configurable and pluggable message handling through log targets;
  • Included console, file, network, and email log targets.

Requirements

Go 1.2 or above.

Installation

Run the following command to install the package:

go get github.com/admpub/log

Getting Started

The following code snippet shows how you can use this package.

package main

import (
	"github.com/admpub/log"
)

func main() {
    // creates the root logger
	logger := log.NewLogger()

	// adds a console target and a file target
	t1 := log.NewConsoleTarget()
	t2 := log.NewFileTarget()
	t2.FileName = "app.log"
	t2.MaxLevel = log.LevelError
	logger.Targets = append(logger.Targets, t1, t2)

	logger.Open()
	defer logger.Close()

	// calls log methods to log various log messages
	logger.Error("plain text error")
	logger.Error("error with format: %v", true)
	logger.Debug("some debug info")

	// customizes log category
	l := logger.GetLogger("app.services")
	l.Info("some info")
	l.Warn("some warning")

	...
}

Loggers and Targets

A logger provides various log methods that can be called by application code to record messages of various severity levels.

A target filters log messages by their severity levels and message categories and processes the filtered messages in various ways, such as saving them in files, sending them in emails, etc.

A logger can be equipped with multiple targets each with different filtering conditions.

The following targets are included in the log package.

  • ConsoleTarget: displays filtered messages to console window
  • FileTarget: saves filtered messages in a file (supporting file rotating)
  • NetworkTarget: sends filtered messages to an address on a network
  • MailTarget: sends filtered messages in emails

You can create a logger, configure its targets, and start to use logger with the following code:

// creates the root logger
logger := log.NewLogger()
logger.Targets = append(logger.Targets, target1, target2, ...)
logger.Open()
...calling log methods...
logger.Close()

Severity Levels

You can log a message of a particular severity level (following the RFC5424 standard) by calling one of the following methods of the Logger struct:

  • Fatal(): fatal conditions.
  • Error(): error conditions.
  • Warn(): warning conditions.
  • Info(): informational purpose.
  • Debug(): debugging purpose.

Message Categories

Each log message is associated with a category which can be used to group messages. For example, you may use the same category for messages logged by the same Go package. This will allow you to selectively send messages to different targets.

When you call log.NewLogger(), a root logger is returned which logs messages using the category named as app. To log messages with a different category, call the GetLogger() method of the root logger or a parent logger to get a child logger and then call its log methods:

logger := log.NewLogger()
// the message is of category "app"
logger.Error("...")

l1 := logger.GetLogger("system")
// the message is of category "system"
l1.Error("...")

l2 := l1.GetLogger("app.models")
// the message is of category "app.models"
l2.Error("...")

Message Formatting

By default, each log message takes this format when being sent to different targets:

2015-10-22T08:39:28-04:00 [Error][app.models] something is wrong
...call stack (if enabled)...

You may customize the message format by specifying your own message formatter when calling Logger.GetLogger(). For example,

logger := log.NewLogger()
logger = logger.GetLogger("app", func (l *Logger, e *Entry) string {
    return fmt.Sprintf("%v [%v][%v] %v%v", e.Time.Format(time.RFC822Z), e.Level, e.Category, e.Message, e.CallStack)
})

Logging Call Stacks

By setting Logger.CallStackDepth as a positive number, it is possible to record call stack information for each log method call. You may further configure Logger.CallStackFilter so that only call stack frames containing the specified substring will be recorded. For example,

logger := log.NewLogger()
// record call stacks containing "myapp/src" up to 5 frames per log message
logger.CallStackDepth = 5
logger.CallStackFilter = "myapp/src"

Message Filtering

By default, messages of all severity levels will be recorded. You may customize Logger.MaxLevel to change this behavior. For example,

logger := log.NewLogger()
// only record messages between Fatal and Warning levels
logger.MaxLevel = log.LevelWarn

Besides filtering messages at the logger level, a finer grained message filtering can be done at target level. For each target, you can specify its MaxLevel similar to that with the logger; you can also specify which categories of the messages the target should handle. For example,

target := log.NewConsoleTarget()
// handle messages between Fatal and Info levels
target.MaxLevel = log.LevelInfo
// handle messages of categories which start with "system.db." or "app."
target.Categories = []string{"system.db.*", "app.*"}

Configuring Logger

When an application is deployed for production, a common need is to allow changing the logging configuration of the application without recompiling its source code. log is designed with this in mind.

For example, you can use a JSON file to specify how the application and its logger should be configured:

{
    "Logger": {
        "Targets": [
            {
                "type": "ConsoleTarget",
            },
            {
                "type": "FileTarget",
                "FileName": "app.log",
                "MaxLevel": 4   // Warning or above
            }
        ]
    }
}

Assuming the JSON file is app.json, in your application code you can use the ozzo-config package to load the JSON file and configure the logger used by the application:

package main

import (
	"github.com/go-ozzo/ozzo-config"
    "github.com/admpub/log"
)

func main() {
    c := config.New()
    c.Load("app.json")
    // register the target types to allow configuring Logger.Targets.
    c.Register("ConsoleTarget", log.NewConsoleTarget)
    c.Register("FileTarget", log.NewFileTarget)

    logger := log.NewLogger()
    if err := c.Configure(logger, "Logger"); err != nil {
        panic(err)
    }
}

To change the logger configuration, simply modify the JSON file without recompiling the Go source files.

# Packages

No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
DefaultFormatter is the default formatter used to format every log message.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
GetCallSingleStack 获取单个记录.
GetCallStack returns the current call stack information as a string.
GetLevel 获取指定名称的等级信息.
No description provided by the author
No description provided by the author
No description provided by the author
HTTPStatusLevelName HTTP状态码相应级别名称.
No description provided by the author
No description provided by the author
IsEnabled 是否启用了某个等级的日志输出.
JSONFormatter json格式.
New creates a new Logger.
NewConsoleTarget creates a ConsoleTarget.
NewFileTarget creates a FileTarget.
No description provided by the author
NewLogger creates a root logger.
NewWriteCloserTarget creates a WriteCloserTarget.
NormalFormatter 标准格式.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ShortFileFormatter 简介文件名格式.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

ActionExit 退出程序.
ActionNothing 无操作.
ActionPanic 触发Panic.
Success, Warning, Error can also be summary items.
No description provided by the author
No description provided by the author
Success, Warning, Error can also be summary items.
Success, Warning, Error can also be summary items.
Success, Warning, Error can also be summary items.
Success, Warning, Error can also be summary items.
Success, Warning, Error can also be summary items.
Success, Warning, Error can also be summary items.
Success, Warning, Error can also be summary items.
Success, Warning, Error can also be summary items.
RFC5424 log message levels.
RFC5424 log message levels.
RFC5424 log message levels.
RFC5424 log message levels.
RFC5424 log message levels.
RFC5424 log message levels.
RFC5424 log message levels.
Success, Warning, Error can also be summary items.
Success, Warning, Error can also be summary items.
STATE INDICATORS.
Success, Warning, Error can also be summary items.

# Variables

target console.
30.
100M.
DefaultLog 默认日志实例.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
LevelNames maps log levels to names.
Levels 所有日志等级.
LevelUppers 日志大写名称前缀.

# Structs

CallStack 调用栈信息.
ConsoleTarget writes filtered log messages to console window.
Entry
Entry represents a log entry.
FileTarget writes filtered log messages to a file.
Filter checks if a log message meets the level and category requirements.
JSONL json格式信息.
Logger records log messages and dispatches them to various targets for further processing.
No description provided by the author
WriteCloserTarget writes filtered log messages to a io.WriteCloser.

# Interfaces

No description provided by the author
Target represents a target where the logger can send log messages to for further processing.

# Type aliases

No description provided by the author
Formatter formats a log message into an appropriate string.
No description provided by the author