Categorygithub.com/redochen/log4go
modulepackage
0.0.0-20230610094231-26e5aea03007
Repository: https://github.com/redochen/log4go.git
Documentation: pkg.go.dev

# README

log4go

Description

This repository is reconstructed from jeanphorn's log4go, which is a logging package similar to log4j for the Go programming language.

Added Flush method, and fixed one bug of FileLogWrite: absolute or relative path for log file.

Features

  • Log to console
  • Log to file, support rotate by size or time
  • log to network, support tcp and udp
  • support xml config

  • Support Json style configuration
  • Add Category for log
    • Classify your logs for different output and different usage.
  • Compatible with the old
  • Support json style config content beside filename

Usage

First, get the code from this repo.

go get github.com/redochen/log4go

Then import it to your project.

import log "github.com/redochen/log4go"

Examples

The config file is optional, if you don't set the config file, it would use the default console config.

Here is a Json config example:

{
    "console": {
        "enable": true,		// wether output the log
        "level": "ERROR"	// log level: DEBUG, INFO, WARNING, ERROR, FATAL
    },
    "files": [{
        "enable": true,
        "level": "DEBUG",
        "filename":"./test.log",
        "category": "Test",			// different category log to different files
        "pattern": "[%D %T] [%C] [%L] (%S) %M"	// log output formmat
    },{ 
        "enable": false,
        "level": "DEBUG",
        "filename":"rotate_test.log",
        "category": "TestRotate",
        "pattern": "[%D %T] [%C] [%L] (%S) %M",
        "rotate": true,				// whether rotate the log
        "maxsize": "500M",
        "maxlines": "10K",
        "daily": true,
        "sanitize": true
    }], 
    "sockets": [{
        "enable": false,
        "level": "DEBUG",
        "category": "TestSocket",
        "pattern": "[%D %T] [%C] [%L] (%S) %M",
        "addr": "127.0.0.1:12124",
        "protocol":"udp"
    }]  
}

Code example:

package main

import (
	log "github.com/redochen/log4go"
)

func main() {
	// load config file, it's optional
	// or log.LoadConfiguration("./example.json", "json")
	// config file could be json or xml
	log.LoadConfiguration("./example.json")

	log.LOGGER("Test").Info("category Test info test ...")
	log.LOGGER("Test").Info("category Test info test message: %s", "new test msg")
	log.LOGGER("Test").Debug("category Test debug test ...")

	// Other category not exist, test
	log.LOGGER("Other").Debug("category Other debug test ...")

	// socket log test
	log.LOGGER("TestSocket").Debug("category TestSocket debug test ...")

	// original log4go test
	log.Info("normal info test ...")
	log.Debug("normal debug test ...")

	log.Close()
}

The output like:

[2017/11/15 14:35:11 CST] [Test] [INFO] (main.main:15) category Test info test ...
[2017/11/15 14:35:11 CST] [Test] [INFO] (main.main:16) category Test info test message: new test msg
[2017/11/15 14:35:11 CST] [Test] [DEBG] (main.main:17) category Test debug test ...
[2017/11/15 14:35:11 CST] [DEFAULT] [INFO] (main.main:26) normal info test ...
[2017/11/15 14:35:11 CST] [DEFAULT] [DEBG] (main.main:27) normal debug test ...

Thanks

Thanks alecthomas and jeanphorn for providing the original resource.

# Packages

No description provided by the author

# Functions

AddFilter Wrapper for (*Logger).AddFilter.
Close Wrapper for (*Logger).Close (closes and removes all logwriters).
Crash Logs the given message and crashes the program.
Crashf Logs the given message and crashes the program.
Debug Utility for debug log messages When given a string as the first argument, this behaves like Logf but with the DEBUG log level (e.g.
Error Utility for error log messages (returns an error for easy function returns) (see Debug() for parameter explanation) These functions will execute a closure exactly once, to build the error message for the return Wrapper for (*Logger).Error.
Exit Compatibility with `log`.
Exitf Compatibility with `log`.
Fatal Utility for fatal log messages (returns an error for easy function returns) (see Debug() for parameter explanation) These functions will execute a closure exactly once, to build the error message for the return Wrapper for (*Logger).Fatal.
Flush Wrapper for (*Logger).Flush (flush all logwriters).
FormatLogRecord Known format codes: %T - Time (15:04:05 MST) %t - Time (15:04) %D - Date (2006/01/02) %d - Date (01/02/06) %L - Level (DBG, INF, WARN, ERR, FTL) %S - Source %M - Message Ignores unknown formats Recommended: "[%D %T] [%L] (%S) %M".
Info Utility for info log messages (see Debug() for parameter explanation) Wrapper for (*Logger).Info.
LoadConfiguration Wrapper for (*Logger).LoadConfiguration.
Log Send a log message manually Wrapper for (*Logger).Log.
Logc Send a closure log message Wrapper for (*Logger).Logc.
Logf Send a formatted log message easily Wrapper for (*Logger).Logf.
NewConsoleLogger creates a new logger with a "stdout" filter configured to send log messages at or above lvl to standard output.
NewConsoleLogWriter This creates a new ConsoleLogWriter.
NewDefaultLogger creates a new logger with a "stdout" filter configured to send log messages at or above lvl to standard output.
NewFileLogWriter creates a new LogWriter which writes to the given file and has rotation enabled if rotate is true.
NewFormatLogWriter This creates a new FormatLogWriter.
NewLogger creates a new logger.
NewSocketLogWriter create new instance of SocketLogWriter.
NewXMLLogWriter is a utility method for creating a FileLogWriter set up to output XML record log messages instead of line-based ones.
PathExists determines if a file or folder existsif bool is true,the file or folder exists.
ReadFile reads file.
Stderr Compatibility with `log`.
Stderrf Compatibility with `log`.
Stdout Compatibility with `log`.
Stdoutf Compatibility with `log`.
Warn Utility for warn log messages (returns an error for easy function returns) (see Debug() for parameter explanation) These functions will execute a closure exactly once, to build the error message for the return Wrapper for (*Logger).Warn.

# Constants

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
Version information.
Version information.
Version information.
Version information.
No description provided by the author

# Variables

Global global logger instance.
LogBufferLength specifies how many log messages a particular log4go logger can buffer at a time before writing them.

# Structs

ConsoleConfig config struct for console log.
ConsoleLogWriter This is the standard writer that prints to standard output.
FileConfig config struct for file log.
FileLogWriter This log writer sends output to a file.
A Filter represents the log level below which no log records are written to the associated LogWriter.
LogConfig presents json log config struct.
A LogRecord contains all of the pertinent information for each message.
SocketConfig config for socket log.

# Interfaces

LogWriter This is an interface for anything that should be able to write logs.

# Type aliases

Level These are the integer logging levels used by the logger.
A Logger represents a collection of Filters through which log messages are written.