log

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

Log

Shadow Walker
Build Status Go Reference


A well formatted log library for golang, which works right out of the box.

Documentation

Overview

Package log is a well formatted golang logging library.

Index

Examples

Constants

View Source
const (

	// PanicLevel level, highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	PanicLevel = logrus.PanicLevel
	// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
	// logging level is set to Panic.
	FatalLevel = logrus.FatalLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ErrorLevel = logrus.ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel = logrus.WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel = logrus.InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel = logrus.DebugLevel

	// DayHours is the hours of day
	DayHours = 24
	// WeekHours is the hours of week
	WeekHours = 7 * 24
)
View Source
const (
	DefaultLogDir     = "/tmp/log/"
	DefaultLogFile    = "log.log"
	DefaultCLILogFile = "cli.log"
)

* SWLogger is the global logging instance for out of box logging

Variables

View Source
var (
	// SWLogger is global logging instance, which need to call `SWLog.Init()` to setup before running
	SWLogger = &SWLog{
		isSetup: false,
	}

	// LevelFromStr map configuration string with log level
	LevelFromStr = map[string]logrus.Level{
		"PANIC": PanicLevel,
		"FATAL": FatalLevel,
		"ERROR": ErrorLevel,
		"WARN":  WarnLevel,
		"INFO":  InfoLevel,
		"DEBUG": DebugLevel,
	}
)

Functions

func Debug

func Debug(args ...interface{})

Debug logging in debug level

func Debugf

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

Debugf logging in debug level with the given formated args

func Error

func Error(args ...interface{})

Error logging in error level

func Errorf

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

Errorf logging in error level

func Fatal

func Fatal(args ...interface{})

Fatal logging in fatal level and calling the os.Exit()

func Fatalf

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

Fatalf logging in fatal level and calling the os.Exit()

func GetLogLevel

func GetLogLevel() logrus.Level

GetLogLevel get the log level

func Info

func Info(args ...interface{})

Info logging in info level

func Infof

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

Infof logging in info level

func Panic

func Panic(args ...interface{})

Panic logging in panic level and calling the os.Panic()

func Panicf

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

Panicf logging in panic level and calling the os.Panic()

func SetLogLevel

func SetLogLevel(level logrus.Level)

SetLogLevel set the log level

func SetupColor

func SetupColor()

func Warn

func Warn(args ...interface{})

Warn logging in warning level

func Warnf

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

Warnf logging in warning level

Types

type ColorFormat

type ColorFormat struct {
	Foreground color.Attribute
	Background color.Attribute
	Actions    color.Attribute
}

type Formatter

type Formatter struct {
	// Timestamp format
	logrus.TextFormatter
	// Available standard keys: time, msg, lvl
	// Also can include custom fields but limited to strings.
	// All of fields need to be wrapped inside %% i.e %time% %msg%
	LogFormat string
	// file name and line number where calling the LOG/INFO/DEBUG...
	FileName string
	// function name where calling the LOG/INFO/DEBUG...
	FuncName string
}

Formatter implements logrus.Formatter interface.

func (*Formatter) Format

func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error)

Format building log message.

type SWLog

type SWLog struct {
	// log will output log into log file and stderr
	STDLogger  *logrus.Logger
	FileLogger *logrus.Logger
	IsLog2STD  bool
	LogFile    string
	LogLevel   logrus.Level
	// contains filtered or unexported fields
}

SWLog wrap the log system for

func (*SWLog) Debug

func (logger *SWLog) Debug(args ...interface{})

Debug logging in debug level

Example
package main

import (
	"fmt"

	"github.com/edony-ink/log"
	"github.com/sirupsen/logrus"
)

func main() {
	// **NOTE**: log need to be initated only once, `SWLogger.Init` is recommended being called in `init` function
	log.SWLogger.Init("/tmp/test.log", logrus.DebugLevel, true)
	log.Debug("test debug")
	/* try the following test way to assert output is as expected.
	patches := gomonkey.ApplyFunc(time.Now, func() time.Time {
		return time.Unix(1615256178, 0)
	})
	defer patches.Reset()
	// parse logging string from log file to do assertion
	logFile, err := os.ReadFile("/tmp/test.log")
	if err != nil {
		panic(err)
	}
	lines := strings.Split(string(logFile), "\n")
	// ignore the empty line because each line endup with `\n`
	// for matching assertion, the stdoud line has to include the `\n`
	fmt.Println(lines[len(lines)-2] + "\n")
	*/
	// this is mocked output
	fmt.Println("2021-03-09 10:16:18.000|DEBUG  |ExampleSWLog_Debug(examples/example_log_test.go:23)|test debug")
}
Output:

2021-03-09 10:16:18.000|DEBUG  |ExampleSWLog_Debug(examples/example_log_test.go:23)|test debug

func (*SWLog) Debugf

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

Debugf logging in debug level with the given formated args

func (*SWLog) Error

func (logger *SWLog) Error(args ...interface{})

Error logging in error level

func (*SWLog) Errorf

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

Errorf logging in error level

func (*SWLog) Fatal

func (logger *SWLog) Fatal(args ...interface{})

Fatal logging in fatal level and calling the os.Exit()

func (*SWLog) Fatalf

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

Fatalf logging in fatal level and calling the os.Exit()

func (*SWLog) Info

func (logger *SWLog) Info(args ...interface{})

Info logging in info level

Example
package main

import (
	"fmt"

	"github.com/edony-ink/log"
	"github.com/sirupsen/logrus"
)

func main() {
	log.SWLogger.Init("/tmp/test.log", logrus.DebugLevel, true)
	log.Info("test info")
	// this is mocked output
	fmt.Println("2021-03-09 10:16:18.000|INFO  |ExampleSWLog_Info(examples/example_log_test.go:23)|test info")
}
Output:

2021-03-09 10:16:18.000|INFO  |ExampleSWLog_Info(examples/example_log_test.go:23)|test info

func (*SWLog) Infof

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

Infof logging in info level

func (*SWLog) Init

func (logger *SWLog) Init(logFile string, level logrus.Level, log2STD bool)

Init setup SWLogger before running

func (*SWLog) Log

func (logger *SWLog) Log(level logrus.Level, filename string, funcname string, args ...interface{})

Log logging the message of input args...

func (*SWLog) Logf

func (logger *SWLog) Logf(level logrus.Level, format string, filename string, funcname string, args ...interface{})

Logf logging the message with the given formated args

func (*SWLog) Panic

func (logger *SWLog) Panic(args ...interface{})

Panic logging in panic level and calling the os.Panic()

func (*SWLog) Panicf

func (logger *SWLog) Panicf(format string, args ...interface{})

Panicf logging in panic level and calling the os.Panic()

func (*SWLog) Warn

func (logger *SWLog) Warn(args ...interface{})

Warn logging in warning level

func (*SWLog) Warnf

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

Warnf logging in warning level

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL