Giter Club home page Giter Club logo

golog's Introduction

Go Reference

golog

Simple logging library for golang

Using

Import

import "github.com/pchchv/golog"

Call golog.{Plain|Info|Debug|Error|Fatal} with a message.

golog.Info("Hello world!")
golog.Debug("Coordinate: %d, %d", x, y)

The default printing format is something like this:

2023-01-11 11:59:23.411 [INFO]  main.go:21 | Hello world!
2023-01-11 11:59:23.412 [DEBUG] main.go:22 | Coordinate: 42, 13

Only the golog.Plain function does not produce leading information (date, log-level, etc.) and just acts like fmt.Printf does.

Error handling

Recommended to use pkg/errors package to create and wrap your errors. It enables you to see stack traces.

To exit on an error, there's the golog.FatalCheck function:

err := someFunctionCall()
golog.FatalCheck(err)

When err is not nil, then the error including stack trace will be printed and your application exists with exit code 1.

Log level

Specify the log level by changing golog.LogLevel. Possible value are golog.LOG_PLAIN, golog.LOG_DEBUG, golog.LOG_INFO, golog.LOG_ERROR and golog.LOG_FATAL.

Depending on the log level, some functions will be quite and do not produce outputs anymore:

log level Methods which will produce an output
LOG_PLAIN golog.Plain()*
golog.Debug()
golog.Info()
golog.Error()
golog.Fatal()
golog.CheckFatal()
golog.Stack()
LOG_DEBUG golog.Debug()
golog.Info()
golog.Error()
golog.Fatal()
golog.CheckFatal()
golog.Stack()
LOG_INFO golog.Info()
golog.Error()
golog.Fatal()
golog.CheckFatal()
golog.Stack()
LOG_ERROR golog.Error()
golog.Fatal()
golog.CheckFatal()
golog.Stack()
LOG_FATAL golog.Fatal()**
golog.CheckFatal()**
* Prints to stdout but without any tags in front
** This will print the error and call os.Exit(1)

Function suffixes / Variants

Some functions have a suffix with slightly different behavior.

For non-fatal functions:

  • Suffix b: Acts like the normal function, but in order to print the correct caller you can go back in the stack.

For fatal-functions:

  • Suffix f: Acts like the normal function, but after printing the stack, the given format string will be evaluated and printed as well.

Change general output format

The format can be changed by implementing the printing function specified in the golog.FormatFunctions array.

Exmaple: To specify your own debug-format:

func main() {
    // Whenever golog.Debug is called, our simpleDebug method is used to produce the output.
    golog.FormatFunctions[golog.LOG_DEBUG] = simpleDebug
    
    golog.Debug("Hello world!")
    }
    
    func simpleDebug(writer *os.File, time, level string, maxLength int, caller, message string) {
    // Don't forget the \n at the end ;)
    fmt.Fprintf(writer, "Debug: %s\n", message)
}

This example will print:

Debug: Hello world!

Change time format

To change only the time format, change the value of the golog.DateFormat variable. The format of this variable if the format described in the time package.

Example:

func main() {
    golog.DateFormat = "02.01.2006 at 15:04:05"
    
    golog.Debug("Hello world!")
}

This will produce:

11.01.2023 at 11:34:22 [DEBUG] main.go:37 | Hello world!

golog's People

Contributors

pchchv avatar

Watchers

 avatar

golog's Issues

Logging to file

Need to implement logging to a file.
I see the implementation as a copy of the string that is sent to the output of the command line, written to the file.

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.