GithubHelp home page GithubHelp logo

vedranvuk / logex Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 44 KB

logex is a logging package with somewhat more control than the stdlib log package and is largely inspired by the logrus README file.

License: MIT License

Go 100.00%
logex logging logger log go golang

logex's Introduction

logex

Package logex is a logging package with somewhat more control than the stdlib log package and is largely inspired by the logrus README file.

API is not yet set.

Interface to a Logger is as follows:

// Log defines an interface for a logger.
type Log interface {

	// Debugf will log a debug message formed from format string and args.
	Debugf(string, ...interface{})
	// Debugln will log args as a debug message.
	Debugln(...interface{})
	// Infof will log an info message formed from format string and args.
	Infof(string, ...interface{})
	// Infoln will log args as an info message.
	Infoln(...interface{})
	// Warningf will log a warning message formed from format string and args.
	Warningf(string, ...interface{})
	// Warningln will log args as a warning message.
	Warningln(...interface{})
	// Errorf will log an error and an error message formed from format string and args.
	Errorf(error, string, ...interface{})
	// Errorln will log an error and args as a warning message.
	Errorln(error, ...interface{})

	// Printf will log a message with a custom logging level formed from format string and args.
	Printf(LogLevel, string, ...interface{})
	// Println will log args as a message with custom logging level.
	Println(LogLevel, ...interface{})

	// ToOutputs will return a clone which will output only to specified output names.
	ToOutputs(names ...string) Log

	// Caller will append the caller field to the next logged line.
	WithCaller(skip int) Log
	// Stack will append the stack field to the next logged line.
	WithStack(skip int, depth int) Log
	// Fields will append the specified fields to the next logged line.
	WithFields(*Fields) Log
}

The following log levels are predefined:

const (
	// LevelNone is undefined logging level. It prints nothing.
	LevelNone LogLevel = iota
	// LevelMute is the silent logging level used to silence the logger.
	LevelMute
	// LevelError is the error logging level that prints errors only.
	LevelError
	// LevelWarning is the warning logging level that prints warnings and errors.
	LevelWarning
	// LevelInfo is the info logging level that prints information, warnings and errors.
	LevelInfo
	// LevelDebug is the debug logging level that prints debug messages, information, warnings and errors.
	LevelDebug
	// LevelCustom and levels up to LevelPrint are custom logging levels.
	// To define a custom logging level use: MyLevel := LogLevel(LevelCustom +1).
	LevelCustom
	// LevelPrint is the print logging level that prints everything that gets logged.
	LevelPrint = LogLevel(255)
)

Custom log levels are defineable in the range LevelCustom+1 ... LevelPrint.

Usage

To create a new logger that outputs to stdout using default simple text formatter use NewStd().

Example:

l := NewStd(nil)
l.Errorln(errors.New("actual error message"), "additional error message")
l.Debugln("debug info)
// Output:
[2020-03-03 12:56:49] Error: additional error message
        actual error message
[2020-03-03 12:59:13] Debug: debug info

To create a new custom logger that formats errors to json and outputs to custom oputput use New().

Example:

l := New(nil)
l.AddOutput("stdout", os.StdOut, NewJSONFormatter(true))
l.Errorln(errors.New("actual error message"), "additional error message")
l.Debugln("debug info)
// Output:
{
        "error": {},
        "loglevel": 2,
        "message": "additional error message\n",
        "time": "2020-03-03T13:00:19.46159898+01:00"
}
{
        "loglevel": 5,
        "message": "debug info\n",
        "time": "2020-03-03T13:00:19.461790209+01:00"
}

You can add additional fields per log line.

Example:

l := New(nil)
l.SetLevel(LevelPrint)
l.AddOutput("stdout", os.Stdout, NewJSONFormatter(true))
f := NewFields()
f.Set("customfield", "customvalue")
l.Fields(f).Println(LevelInfo, "some log message")
// Output:
{
        "customfield": "customvalue",
        "loglevel": 255,
        "message": "some log message\n",
        "time": "2020-03-03T13:05:46.550892961+01:00"
}

You can also create custom formatters.

// Formatter formats Fields to a custom format.
type Formatter interface {
	// Format must return a string representation of Fields, such as JSON, CSV, Text, etc..
	Format(*Fields) string
}

License

See included LICENSE file.

logex's People

Contributors

vedranvuk 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.