Categorygithub.com/jeanphorn/log4go
modulepackage
0.0.0-20231225120528-d93eb9001e51
Repository: https://github.com/jeanphorn/log4go.git
Documentation: pkg.go.dev

# README

log4go

Description

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

Two new features are supported: one is Json config style, and the other is different output according to category.

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/jeanphorn/log4go

Then import it to your project.

import log "github.com/jeanphorn/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": "FINE"		// log level: FINE, DEBUG, TRACE, INFO, WARNING,ERROR, CRITICAL
    },  
    "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/jeanphorn/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 for providing the original resource.

# Packages

No description provided by the author

# Functions

Wrapper for (*Logger).AddFilter.
Wrapper for (*Logger).Close (closes and removes all logwriters).
No description provided by the author
Logs the given message and crashes the program.
Utility for critical 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).Critical.
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.
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.
Compatibility with `log`.
Compatibility with `log`.
Utility for fine log messages (see Debug() for parameter explanation) Wrapper for (*Logger).Fine.
Utility for finest log messages (see Debug() for parameter explanation) Wrapper for (*Logger).Finest.
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 (FNST, FINE, DEBG, TRAC, WARN, EROR, CRIT) %S - Source %M - Message Ignores unknown formats Recommended: "[%D %T] [%L] (%S) %M".
Utility for info log messages (see Debug() for parameter explanation) Wrapper for (*Logger).Info.
Wrapper for (*Logger).LoadConfiguration.
Send a log message manually Wrapper for (*Logger).Log.
Send a closure log message Wrapper for (*Logger).Logc.
Send a formatted log message easily Wrapper for (*Logger).Logf.
LOGGER get the log Filter by category.
Create a new logger with a "stdout" filter configured to send log messages at or above lvl to standard output.
This creates a new ConsoleLogWriter.
Create 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.
This creates a new FormatLogWriter.
Create a new logger.
No description provided by the author
NewXMLLogWriter is a utility method for creating a FileLogWriter set up to output XML record log messages instead of line-based ones.
No description provided by the author
Compatibility with `log`.
Compatibility with `log`.
Compatibility with `log`.
Compatibility with `log`.
Utility for trace log messages (see Debug() for parameter explanation) Wrapper for (*Logger).Trace.
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
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
No description provided by the author

# Variables

No description provided by the author
LogBufferLength specifies how many log messages a particular log4go logger can buffer at a time before writing them.

# Structs

No description provided by the author
This is the standard writer that prints to standard output.
No description provided by the author
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.
No description provided by the author

# Interfaces

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

# Type aliases

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