logger

package module
v0.0.0-...-fb5d91d Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2019 License: Apache-2.0 Imports: 13 Imported by: 0

README

logger

Package to write log files

Main usage:
Log, _:= logger.New("/path/of/file.log", logger.LEVEL_ALL, true)
Log.Printf(logger.INFO, "Text formatted to write in log string (%s) int (%d)", "value", 90)
Log.Println(logger.INFO, "Text to write in log")

This package implements the following log types: ACCESS, FATAL, ERROR, WARN, INFO, DEBUG.

Right after a FATAL log is written, the application will be killed with a Exit (1)

To initialize logger its necessary to define which types can be written. This package has 2 log levels.

LEVEL_ALL: Writes all log types

LEVEL_PRODUCTION: Writes all log types except DEBUG

This allows you to initialize the log according to the environment in which your application is running (eg development and production)

Custom Log Levels:

A custom level can be defined using bitwise. Example:

Log, _ := logger.New("/path/of/file.log", logger.INFO|logger.WARN, true)

In this case, only INFO and WARN logs will be written.

Log.Println(logger.INFO, "Text info")
Log.Println(logger.WARN, "Text warn")
Log.Println(logger.ERROR, "Text error") // this won't be logged
File rotation:

At the end of the day, log files will be compressed and a new empty log file will be created. Compression is done using gzip, but can be changed at any time to zip. To change the compression method, use the functions:

Log.SetCompressModeGzip()
Log.SetCompressModeZip()
Stack:

By default, ERROR and WARN logs write a stack stating where the log was written. Example:

func testWriteLog() {
  Log.Printf(ERROR, "test write stack")
}

Will log: 2018/06/20 22:42:24 ERROR [testWriteLog main.go 19] test write stack

To change this setting use the function

Log.SetStackTrace(logger.WARN|logger.INFO)
Contributors

Christopher Madalosso Burin

Documentation

Index

Constants

View Source
const (
	ACCESS = 1 << iota // 1
	FATAL              // 2
	ERROR              // 4
	WARN               // 8
	INFO               // 16
	DEBUG              // 32
)
View Source
const (
	TIME_SYNC          = time.Second
	LOG_FLAGS          = log.LstdFlags
	DEFAULT_MAXDEPTH   = 3
	DEFAULT_STACKTRACE = true

	LEVEL_ALL        = ACCESS | FATAL | ERROR | WARN | INFO | DEBUG
	LEVEL_PRODUCTION = LEVEL_ALL ^ DEBUG

	DEFAULT_WRITESTACKTRACE = ERROR | WARN
)
View Source
const (
	COMPRESS_GZIP = iota
	COMPRESS_ZIP
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Struct Logger

func New

func New(fp string, level int, rotate bool) (l *Logger, err error)

Creates a new instance of logger fp is file with complete path. Ex: /var/log/mylog.log level define which log types to write to the file. Ex: logger.FATAL|logger.INFO. rotate define whether to rotate the log automatically or not

func (*Logger) Close

func (l *Logger) Close()

Close log file

func (*Logger) Fatal

func (l *Logger) Fatal(txt ...interface{})

Same as the Print function but after writing to the file, it kills the application with an Exit (1)

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, txt ...interface{})

Same as the Printf function but after writing to the file, it kills the application with an Exit (1)

func (*Logger) Fatalln

func (l *Logger) Fatalln(txt ...interface{})

Same as the Println function but after writing to the file, it kills the application with an Exit (1)

func (*Logger) GetTypeString

func (l *Logger) GetTypeString(typeLog int) string

func (*Logger) Print

func (l *Logger) Print(typeLog int, txt ...interface{})

typeLog is a level log. Ex:INFO, ERROR, WARN txt is a message to write

func (*Logger) Printf

func (l *Logger) Printf(typeLog int, format string, txt ...interface{})

typeLog is a level log. Ex:INFO, ERROR, WARN format is text format txt is variables that will be formatted

func (*Logger) Println

func (l *Logger) Println(typeLog int, txt ...interface{})

typeLog is a level log. Ex:INFO, ERROR, WARN txt is a message to write

func (*Logger) SetCompressModeGzip

func (l *Logger) SetCompressModeGzip()

define gzip as compression mode note: works only with active file rotation

func (*Logger) SetCompressModeZip

func (l *Logger) SetCompressModeZip()

define zip as compression mode note: works only with active file rotation

func (*Logger) SetLevel

func (l *Logger) SetLevel(lvl int)

Define which logs types should be save in log file

func (*Logger) SetMaxDepth

func (l *Logger) SetMaxDepth(m int)

define max depth to write stack

func (*Logger) SetOutputTerm

func (l *Logger) SetOutputTerm()

Public Methods Set's

func (*Logger) SetRemoveAfter

func (l *Logger) SetRemoveAfter(day uint16)

define max day to remove files after rotate

func (*Logger) SetStackTrace

func (l *Logger) SetStackTrace(list int)

define log types write stack trace

func (*Logger) SetTimeSync

func (l *Logger) SetTimeSync(t time.Duration)

Define sync time to save log in file

func (*Logger) WritePanic

func (l *Logger) WritePanic(rec interface{}, stack []byte)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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