Git Product home page Git Product logo

logger's Introduction

Logger GoDoc

This is a simple layer on top of the standard log package. Its main purpose is to give the developer separate log streams and logging severity levels.

The goal of this library is to be as simple as possible to use. It only adds the feature mentioned in the previous paragraph and nothing else.

You can see this package's documentation at godoc.org. It is much more than what you will find in this README.

Usage

Logging is as simple as

import (
    "github.com/ironsmile/logger"
)

func main() {
    logger.Errorf("I have an %d errors", 5)
    logger.SetLevel(logger.LevelDebug)
    logger.Debugln("A debug with a new line")
}

The above example uses the default logger. You can create as many loggers as you want:

func main() {
    lgStdOut := logger.New()
    lgStdOut.Log("Log object which logs to the stdout")

    outFile, _ := os.Create("/tmp/file.log")
    lgFile := logger.New()
    lgFile.SetLogOutput(outFile)
    lgFile.Log("This will be written in the log file")

    lgFile.SetErrorOutput(os.Stdout)
    lgFile.Errorln("This will be in the standard output")
}

You can take a look at the example file which showcases different usage scenarios.

Interface

Loggers support the Debug(f|ln)?, Log(f|ln)?, Error(f|ln)? and Fatal(f|ln)? functions which handle their arguments in the way fmt.Print(f|ln)? do. For more info regarding their arguments see the documentation.

A Logger is made up of 3 different exported log.Logger instances. Every one of them represents a logging stream. The Logger structure looks like this:

type Logger struct {
    Debugger *log.Logger
    Logger   *log.Logger
    Errorer  *log.Logger
    Level    int
}

As expected Debugger represents the debug stream, Logger - the logging and Errorer - the log stream. Being exported they can be used directly, possibly for configuration.

Logging Levels

Every Logger has a log level. There are 4 possible levels - LevelDebug, LevelLog, LevelError, LevelNoLog. See the package documentation for information on their usage.

Configuration

Use SetDebugOutput, SetLogOutput and SetErrorOutput to change the stream destination.

You can change the logging level by setting the Level property of a logger:

lg := logger.New()
lg.Level = logger.LevelDebug

For everything else you can access the underlying log.Logger instances directly.

Fatal functions

They do not have their own output stream but use the error stream. So there is no function SetFatalOutput and there is no property Fataller in the logger struct.

Underneath they actually use the log.Fatal functions which means a call to logger.Fatal(f|ln)? will print the message and halt the program.

logger's People

Contributors

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