README

go-log

Build Status Go Report Card Documentation license GitHub version GitHub issues

This is a Golang library with logging related functions which I use in my different projects.

Usage

package main

import (
    "github.com/pieterclaerhout/go-log"
)

func main() {

    log.DebugMode = true
    log.DebugSQLMode = true
    log.PrintTimestamp = true
    log.PrintColors = true
    log.TimeFormat = "2006-01-02 15:04:05.000"

    myVar := map[string]string{"hello": "world"}

    log.Debug("arg1", "arg2")
    log.Debugf("arg1 %d", 1)
    log.DebugDump(myVar, "prefix")
    log.DebugSeparator("title")
    log.DebugSQL("select * from mytable")

    log.Info("arg1", "arg2")
    log.Infof("arg1 %d", 1)
    log.InfoDump(myVar, "prefix")
    log.InfoSeparator("title")

    log.Warn("arg1", "arg2")
    log.Warnf("arg1 %d", 1)
    log.WarnDump(myVar, "prefix")
    log.WarnSeparator("title")

    log.Error("arg1", "arg2")
    log.Errorf("arg1 %d", 1)
    log.ErrorDump(myVar, "prefix")
    log.ErrorSeparator("title")

    log.Fatal("arg1", "arg2")
    log.Fatalf("arg1 %d", 1)

    err1 := funcWithError()
    log.StackTrace(err1)

    err2 := funcWithError()
    log.CheckError(err2)

}

Environment variables

The defaults are taken from the environment variables:

  • DEBUG: log.DebugMode
  • DEBUG_SQL: log.DebugSQLMode
  • PRINT_TIMESTAMP: log.PrintTimestamp
Expand ▾ Collapse ▴

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DebugMode = false

    DebugMode indicates if debug information should be printed or not

    If the environment variable called DEBUG is set to 1, this will default to true. In all other cases, debug mode is false by default.

    View Source
    var DebugSQLMode = false

      DebugSQLMode indicates if the SQL statements should be logged as debug messages

      View Source
      var DefaultTimeFormat = "2006-01-02 15:04:05.000"

        DefaultTimeFormat is the default format to use for the timestamps

        View Source
        var OsExit = os.Exit

          OsExit is the function to exit the app when a fatal error happens

          View Source
          var PrintColors = false

            PrintColors indicates if the messages should be printed in color or not

            View Source
            var PrintTimestamp = false

              PrintTimestamp indicates if the log messages should include a timestamp or not

              View Source
              var Stderr io.Writer = os.Stderr

                Stderr is the writer to where the stderr messages should be written (defaults to os.Stderr)

                View Source
                var Stdout io.Writer = os.Stdout

                  Stdout is the writer to where the stdout messages should be written (defaults to os.Stdout)

                  View Source
                  var TestingTimeFormat = "test"

                    TestingTimeFormat is the format to use for the timestamps during testing

                    View Source
                    var TimeFormat = DefaultTimeFormat

                      TimeFormat is the format to use for the timestamps

                      View Source
                      var TimeZone *time.Location

                        TimeZone indicates in which timezone the time should be formatted

                        Functions

                        func CheckError

                        func CheckError(err error)

                          CheckError checks if the error is not nil and if that's the case, it will print a fatal message and exits the program with exit code 1.

                          If DebugMode is enabled a stack trace will also be printed to stderr

                          func Debug

                          func Debug(args ...interface{})

                            Debug prints a debug message

                            Only shown if DebugMode is set to true

                            func DebugDump

                            func DebugDump(arg interface{}, prefix string)

                              DebugDump dumps the argument as a debug message with an optional prefix

                              func DebugSQL

                              func DebugSQL(sql string)

                                DebugSQL formats the SQL statement and prints it as a debug message

                                Only shown if DebugMode and DebugSQLMode are set to true

                                func DebugSeparator

                                func DebugSeparator(args ...interface{})

                                  DebugSeparator prints a debug separator

                                  Only shown if DebugMode is set to true

                                  func Debugf

                                  func Debugf(format string, args ...interface{})

                                    Debugf prints a debug message with a format and arguments

                                    Only shown if DebugMode is set to true

                                    func Error

                                    func Error(args ...interface{})

                                      Error prints an error message to stderr

                                      func ErrorDump

                                      func ErrorDump(arg interface{}, prefix string)

                                        ErrorDump dumps the argument as an err message with an optional prefix to stderr

                                        func ErrorSeparator

                                        func ErrorSeparator(args ...interface{})

                                          ErrorSeparator prints an error separator

                                          func Errorf

                                          func Errorf(format string, args ...interface{})

                                            Errorf prints an error message with a format and arguments

                                            func Fatal

                                            func Fatal(args ...interface{})

                                              Fatal logs a fatal error message to stdout and exits the program with exit code 1

                                              func Fatalf

                                              func Fatalf(format string, args ...interface{})

                                                Fatalf prints a fatal message with a format and arguments

                                                func FormattedStackTrace

                                                func FormattedStackTrace(err error) string

                                                  FormattedStackTrace returns a formatted stacktrace for err

                                                  func Info

                                                  func Info(args ...interface{})

                                                    Info prints an info message

                                                    func InfoDump

                                                    func InfoDump(arg interface{}, prefix string)

                                                      InfoDump dumps the argument as an info message with an optional prefix

                                                      func InfoSeparator

                                                      func InfoSeparator(args ...interface{})

                                                        InfoSeparator prints an info separator

                                                        func Infof

                                                        func Infof(format string, args ...interface{})

                                                          Infof prints an info message with a format and arguments

                                                          func StackTrace

                                                          func StackTrace(err error)

                                                            StackTrace prints an error message with the stacktrace of err to stderr

                                                            func Warn

                                                            func Warn(args ...interface{})

                                                              Warn prints an warning message

                                                              func WarnDump

                                                              func WarnDump(arg interface{}, prefix string)

                                                                WarnDump dumps the argument as a warning message with an optional prefix

                                                                func WarnSeparator

                                                                func WarnSeparator(args ...interface{})

                                                                  WarnSeparator prints a warning separator

                                                                  func Warnf

                                                                  func Warnf(format string, args ...interface{})

                                                                    Warnf prints a warning message with a format and arguments

                                                                    Types

                                                                    This section is empty.

                                                                    Directories

                                                                    Path Synopsis
                                                                    cmd