Git Product home page Git Product logo

log4go's Introduction

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 deferent output accordding 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

Usage

First, get the code from this repo.

go get github.com/panho66/log4go

Then import it to you project.

import log "github.com/panho66/log4go"

Examples

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

Here it 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,								// wether rotate the log
        "maxsize": "500M",
        "maxlines": "10K",
        "daily": 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/panho66/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("nomal info test ...")
	log.Debug("nomal 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) nomal info test ...
[2017/11/15 14:35:11 CST] [DEFAULT] [DEBG] (main.main:27) nomal debug test ...

Thanks

Thanks alecthomas for providing the original resource.

log4go's People

Contributors

jeanphorn avatar laptua avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.