logger

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2023 License: MIT Imports: 6 Imported by: 2

README

logger

A lightweight, auto-rolling logger for Go.

Options
// Options is the option set for Logger.
type Options struct {
	// Stdout sets the writer as stdout if it is true.
	Stdout bool

	// ConsoleMode sets logger to be the console mode which claims the logger encoder type as console.
	ConsoleMode bool

	// Filename is the file to write logs to.  Backup log files will be retained
	// in the same directory.
	Filename string

	// MaxSize is the maximum size in megabytes of the log file before it gets rotated.
	MaxSize int

	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int

	// MaxBackups is the maximum number of old log files to retain. The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int

	// Level is a logging priority. Higher levels are more important.
	Level Level

	// Skip is the number of callers skipped by caller annotation
	Skip int
}
Example
package main

import "github.com/chenjiandongx/logger"

// InitLogger initializes the logger
func InitLogger() {
	// feel free to config the options.
	logger.SetOptions(logger.Options{
		Filename:   "/data/log/awesome-project/applog",
		MaxSize:    1000, // 1GB
		MaxAge:     3,    // 3 days
		MaxBackups: 3,    // 3 backups
	})
}

func main() {
	// Note: init logger when you want to run your program in the production env.
	// for example:
	InitLogger()

	// use logger Method anywhere you want directly, such as Info/Warn/Error/...
	// logs will be displayed on the stdout stream by default.
	logger.Info("This is the info level message.")
	logger.Warnf("This is the warn level message. %s", "oop!")
	logger.Error("Something error here.")
}

LICENSE

MIT ©chenjiandongx

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug uses fmt.Sprint to construct and log a message.

func Debugf

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

Debugf uses fmt.Sprintf to log a templated message.

func Error

func Error(args ...interface{})

Error uses fmt.Sprint to construct and log a message.

func Errorf

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

Errorf uses fmt.Sprintf to log a templated message.

func Fatal

func Fatal(args ...interface{})

Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.

func Fatalf

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

Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.

func Info

func Info(args ...interface{})

Info uses fmt.Sprint to construct and log a message.

func Infof

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

Infof uses fmt.Sprintf to log a templated message.

func Panic

func Panic(args ...interface{})

Panic uses fmt.Sprint to construct and log a message, then panics.

func Panicf

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

Panicf uses fmt.Sprintf to log a templated message, then panics.

func Printf

func Printf(template string, args ...interface{})

Printf is the alias for Infof

func Println

func Println(args ...interface{})

Println is the alias for Info

func SetOptions

func SetOptions(opt Options)

SetOptions sets the options for the standard logger.

func Warn

func Warn(args ...interface{})

Warn uses fmt.Sprint to construct and log a message.

func Warnf

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

Warnf uses fmt.Sprintf to log a templated message.

Types

type Level

type Level int8
const (
	// DebugLevel logs are typically voluminous, and are usually disabled in
	// production.
	DebugLevel Level = iota - 1

	// InfoLevel is the default logging priority.
	InfoLevel

	// WarnLevel logs are more important than Info, but don't need individual
	// human review.
	WarnLevel

	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	ErrorLevel

	// DPanicLevel logs are particularly important errors. In development the
	// logger panics after writing the message.
	DPanicLevel

	// PanicLevel logs a message, then panics.
	PanicLevel

	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel
)

type Logger

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

func New

func New(opt Options) Logger

New returns the logger instance with Production Config by default.

func StandardLogger

func StandardLogger() Logger

StandardLogger returns the standard logger with stdout output.

func With

func With(args ...interface{}) Logger

With adds a variadic number of fields to the logging context. It accepts a mix of strongly-typed Field objects and loosely-typed key-value pairs. When processing pairs, the first element of the pair is used as the field key and the second as the field value.

func (Logger) Debug

func (l Logger) Debug(args ...interface{})

Debug uses fmt.Sprint to construct and log a message.

func (Logger) Debugf

func (l Logger) Debugf(template string, args ...interface{})

Debugf uses fmt.Sprintf to log a templated message.

func (Logger) Error

func (l Logger) Error(args ...interface{})

Error uses fmt.Sprint to construct and log a message.

func (Logger) Errorf

func (l Logger) Errorf(template string, args ...interface{})

Errorf uses fmt.Sprintf to log a templated message.

func (Logger) Fatal

func (l Logger) Fatal(args ...interface{})

Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.

func (Logger) Fatalf

func (l Logger) Fatalf(template string, args ...interface{})

Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.

func (Logger) Info

func (l Logger) Info(args ...interface{})

Info uses fmt.Sprint to construct and log a message.

func (Logger) Infof

func (l Logger) Infof(template string, args ...interface{})

Infof uses fmt.Sprintf to log a templated message.

func (Logger) Panic

func (l Logger) Panic(args ...interface{})

Panic uses fmt.Sprint to construct and log a message, then panics.

func (Logger) Panicf

func (l Logger) Panicf(template string, args ...interface{})

Panicf uses fmt.Sprintf to log a templated message, then panics.

func (Logger) Printf

func (l Logger) Printf(template string, args ...interface{})

Printf is the alias for Infof

func (Logger) Println

func (l Logger) Println(args ...interface{})

Println is the alias for Info

func (Logger) Warn

func (l Logger) Warn(args ...interface{})

Warn uses fmt.Sprint to construct and log a message.

func (Logger) Warnf

func (l Logger) Warnf(template string, args ...interface{})

Warnf uses fmt.Sprintf to log a templated message.

func (Logger) With

func (l Logger) With(args ...interface{}) Logger

With adds a variadic number of fields to the logging context. It accepts a mix of strongly-typed Field objects and loosely-typed key-value pairs. When processing pairs, the first element of the pair is used as the field key and the second as the field value.

type Options

type Options struct {
	// Stdout sets the writer as stdout if it is true.
	Stdout bool

	// ConsoleMode sets logger to be the console mode which claims the logger encoder type as console.
	ConsoleMode bool

	// Filename is the file to write logs to.  Backup log files will be retained
	// in the same directory.
	Filename string

	// MaxSize is the maximum size in megabytes of the log file before it gets rotated.
	MaxSize int

	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int

	// MaxBackups is the maximum number of old log files to retain. The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int

	// Level is a logging priority. Higher levels are more important.
	Level Level

	// Skip is the number of callers skipped by caller annotation
	Skip int
}

Options is the option set for Logger.

Jump to

Keyboard shortcuts

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