log

package module
v0.0.0-...-52c21e0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2020 License: BSD-2-Clause Imports: 16 Imported by: 2

README

golog

A full functional log package for golang with following features

  • enhanced but compatible with official log package. Simply using import alias to replace official log
package main

import (
    "bytes"
    "fmt"
    "github.com/mysqto/log"
)

func main() {
    var (
        buf    bytes.Buffer
        logger = log.New(&buf, "INFO: ", log.Lshortfile)

        infof = func(info string) {
            logger.Output(2, info)
        }
    )

    infof("Hello world")

    fmt.Print(&buf)
}

Documentation

Overview

Package log implements a simple logging package. It defines a type, Logger, with methods for formatting output. It also has a predefined 'standard' Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and Panic[f|ln], which are easier to use than creating a Logger manually. That logger writes to standard error and prints the date and time of each logged message. Every log message is output on a separate line: if the message being printed does not end in a newline, the logger will add one. The Fatal functions call os.Exit(1) after writing the log message. The Panic functions call panic after writing the log message.

Index

Examples

Constants

View Source
const (
	Ldate         = 1 << iota     // the date in the local time zone: 2009/01/23
	Ltime                         // the time in the local time zone: 01:23:23
	Lmicroseconds                 // microsecond resolution: 01:23:23.123123.  assumes Ltime.
	Llongfile                     // full file name and line number: /a/b/c/d.go:23
	Lshortfile                    // final file name element and line number: d.go:23. overrides Llongfile
	LUTC                          // if Ldate or Ltime is set, use UTC rather than the local time zone
	Lloggername                   // logger name, Logger.name if empty use process name
	Lgoroutineid                  // current goroutine id from the runtime stack which will cause significant performance reduction, do not use it unless you must
	Llongfunc                     // long function name with vendor & package: github.com/mysqto/golog.(*.Logger).printf
	Lshortfunc                    // short function name: printf
	Lsequence                     // write log sequence id
	Lcolor                        // colorful log when output is tty
	LstdFlags     = Ldate | Ltime // initial values for the standard logger
	Lfull         = Ldate | Ltime | Lmicroseconds | Lshortfile | Lloggername | Lshortfunc | Lsequence | Lcolor
)

These flags define which text to prefix to each log entry generated by the Logger. Bits are or'ed together to control what's printed. There is no control over the order they appear (the order listed here) or the format they present (as described in the comments). The prefix is followed by a colon only when Llongfile or Lshortfile is specified. For example, flags Ldate | Ltime (or LstdFlags) produce,

2009/01/23 01:23:23 message

while flags Ldate | Ltime | Lmicroseconds | Llongfile produce,

2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message

Variables

This section is empty.

Functions

func Debug

func Debug(v ...interface{})

Debug prints debug log.

func Debugf

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

Debugf prints formatted debug log.

func Debugln

func Debugln(v ...interface{})

Debugln prints debug log with newline.

func Error

func Error(v ...interface{})

Error prints error log.

func Errorf

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

Errorf prints formatted error log.

func Errorln

func Errorln(v ...interface{})

Errorln prints error log with newline.

func Fatal

func Fatal(v ...interface{})

Fatal prints fatal log and exit current process.

func Fatalf

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

Fatalf prints formatted fatal log and exit current process.

func Fatalln

func Fatalln(v ...interface{})

Fatalln prints fatal log with newline and exit current process.

func Flags

func Flags() int

Flags returns the output flags for std logger.

func Flush

func Flush()

Flush std logger

func Info

func Info(v ...interface{})

Info prints info log.

func Infof

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

Infof prints formatted info log.

func Infoln

func Infoln(v ...interface{})

Infoln prints info log with newline.

func Name

func Name() string

Name returns the name of the std logger

func Prefix

func Prefix() string

Prefix returns the output prefix for the std logger.

func Print

func Print(v ...interface{})

Print calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Print.

func Printf

func Printf(format string, v ...interface{})

Printf calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Printf.

func Println

func Println(v ...interface{})

Println calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Println.

func SetFlags

func SetFlags(flag int)

SetFlags sets the output flags for the logger.

func SetLogLevel

func SetLogLevel(level Level)

SetLogLevel update the std logger level

func SetName

func SetName(name string)

SetName sets the name for the std logger.

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets the output destination for the std logger.

func SetPrefix

func SetPrefix(prefix string)

SetPrefix sets the output prefix for the std logger.

func Warn

func Warn(v ...interface{})

Warn prints warning log.

func Warnf

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

Warnf prints formatted warning log.

func Warnln

func Warnln(v ...interface{})

Warnln prints warning log with newline.

func Writer

func Writer() io.Writer

Writer returns the output destination for the std logger.

Types

type AsyncLog

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

AsyncLog an async logger

func (*AsyncLog) Flush

func (l *AsyncLog) Flush()

Flush the current log backend

func (*AsyncLog) SetWriter

func (l *AsyncLog) SetWriter(w io.Writer)

SetWriter set the io.Writer of current logger

func (*AsyncLog) Writer

func (l *AsyncLog) Writer() io.Writer

Writer returns the io.Writer of current Syslog

type Backend

type Backend interface {
	// Writer returns the io.Writer of current backend for compatible with golang's log package
	Writer() io.Writer

	// SetWriter sets the io.Writer of current backend for compatible with golang's log package
	SetWriter(w io.Writer)

	// Flush flushes current logging backend
	Flush()
	// contains filtered or unexported methods
}

Backend a logger must implement the backend interface

func NewAsyncBackend

func NewAsyncBackend(w io.Writer) Backend

NewAsyncBackend creates a new async backend

func NewRotateBackend

func NewRotateBackend(filename string, maxFiles int, maxSize ByteSize, compress CompressMethod) Backend

NewRotateBackend creates a rotate logger backend with given parameters

func NewSyncBackend

func NewSyncBackend(w io.Writer) Backend

NewSyncBackend create a new sync backend

func NewSyslogBackend

func NewSyslogBackend(level Level, prefix string) (Backend, error)

NewSyslogBackend creates a syslog backend

type ByteSize

type ByteSize int64

ByteSize represent file ByteSize in byte

const (

	// KB = 1 kb bytes
	KB ByteSize
	// MB = 1 mb bytes
	MB
	// GB = 1 gb bytes
	GB
	// TB = 1 tb bytes
	TB
	// PB = 1 pb bytes
	PB
	// EB = 1 eb bytes
	EB
)

type CompressMethod

type CompressMethod int

CompressMethod represent the compress method when archive logs

const (
	NoCompress CompressMethod = iota
	GZIP
	Zlib
	LZW
)

compress method

func (CompressMethod) String

func (c CompressMethod) String() string

String implements the stringer interface

type Handler

type Handler interface {
	Fd() uintptr
}

Handler a file handler interface

type Level

type Level uint32

Level log level

const (
	FATAL Level
	ERROR
	WARN
	INFO
	DEBUG
)

supported levels

type Logger

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

A Logger represents an active logging object that generates lines of output to an io.Writer. Each logging operation makes a single call to the Writer's Write method. A Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Writer.

Example
var (
	buf    bytes.Buffer
	logger = New(&buf, "logger: ", Lshortfile)
)

logger.Print("Hello, log file!")

fmt.Print(&buf)
Output:

logger: example_test.go:18: Hello, log file!

func New

func New(out io.Writer, prefix string, flag int) *Logger

New creates a new Logger. The out variable sets the destination to which log data will be written. The prefix appears at the beginning of each generated log line. The flag argument defines the logging properties.

func NewAsyncLogger

func NewAsyncLogger(w io.Writer, prefix string, flag int) *Logger

NewAsyncLogger creates a new async logger with a io.Writer

func NewRotateLogger

func NewRotateLogger(level Level, prefix string, maxSize ByteSize, compress CompressMethod, flag int) *Logger

NewRotateLogger creates a rotate logger with given log level and flags

func NewSyslog

func NewSyslog(level Level, prefix string, flag int) *Logger

NewSyslog crete a new logger using syslog backend with level/prefix/flag

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

Debug prints debug log.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

Debugf prints formatted debug log.

func (*Logger) Debugln

func (l *Logger) Debugln(v ...interface{})

Debugln prints debug log with newline.

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

Error prints error log.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

Errorf prints formatted error log.

func (*Logger) Errorln

func (l *Logger) Errorln(v ...interface{})

Errorln prints error log with newline.

func (*Logger) Fatal

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

Fatal prints fatal log and exit current process.

func (*Logger) Fatalf

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

Fatalf prints formatted fatal log and exit current process.

func (*Logger) Fatalln

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

Fatalln prints fatal log with newline and exit current process.

func (*Logger) Flags

func (l *Logger) Flags() int

Flags returns the output flags for the logger.

func (*Logger) Flush

func (l *Logger) Flush()

Flush flush current logger

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

Info prints info log.

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

Infof prints formatted info log.

func (*Logger) Infoln

func (l *Logger) Infoln(v ...interface{})

Infoln prints info log with newline.

func (*Logger) Level

func (l *Logger) Level() Level

Level returns current level of logger

func (*Logger) Name

func (l *Logger) Name() string

Name returns the name of current logger

func (*Logger) Output

func (l *Logger) Output(calldepth int, s string) error

Output writes the output for a logging event. The string s contains the text to print after the prefix specified by the flags of the Logger. A newline is appended if the last character of s is not already a newline. Calldepth is used to recover the PC and is provided for generality, although at the moment on all pre-defined paths it will be 2.

Example
var (
	buf    bytes.Buffer
	logger = New(&buf, "INFO: ", Lshortfile)

	infoF = func(info string) {
		_ = logger.Output(3, info)
	}
)

infoF("Hello world")

fmt.Print(&buf)
Output:

INFO: example_test.go:31: Hello world

func (*Logger) Prefix

func (l *Logger) Prefix() string

Prefix returns the output prefix for the logger.

func (*Logger) Print

func (l *Logger) Print(v ...interface{})

Print calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Printf

func (l *Logger) Printf(format string, v ...interface{})

Printf calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Println

func (l *Logger) Println(v ...interface{})

Println calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Println.

func (*Logger) SetFlags

func (l *Logger) SetFlags(flag int)

SetFlags sets the output flags for the logger.

func (*Logger) SetLogLevel

func (l *Logger) SetLogLevel(level Level)

SetLogLevel update the logger's level

func (*Logger) SetName

func (l *Logger) SetName(name string)

SetName sets the name for the logger.

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

SetOutput sets the output destination for the logger.

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(prefix string)

SetPrefix sets the output prefix for the logger.

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

Warn prints warning log.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...interface{})

Warnf prints formatted warning log.

func (*Logger) Warnln

func (l *Logger) Warnln(v ...interface{})

Warnln prints warning log with newline.

func (*Logger) Writer

func (l *Logger) Writer() io.Writer

Writer returns the output destination for the logger.

type Record

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

Record represents a log record and contains the timestamp when the record was created, an increasing id, filename and line and finally the actual formatted log line.

type RotateLogger

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

RotateLogger represents an log backend supporting log rotating and compress

func (*RotateLogger) Flush

func (l *RotateLogger) Flush()

Flush the current log backend

func (*RotateLogger) SetWriter

func (l *RotateLogger) SetWriter(w io.Writer)

SetWriter set the io.Writer of current logger

func (*RotateLogger) Writer

func (l *RotateLogger) Writer() io.Writer

Writer returns the io.Writer of current logger

type SyncLog

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

SyncLog write log synchronously

func (*SyncLog) Flush

func (l *SyncLog) Flush()

Flush the current log backend

func (*SyncLog) SetWriter

func (l *SyncLog) SetWriter(w io.Writer)

SetWriter set the io.Writer of current logger

func (*SyncLog) Writer

func (l *SyncLog) Writer() io.Writer

Writer returns the io.Writer of current logger

type Syslog

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

Syslog is the backend using syslog

func (*Syslog) Flush

func (l *Syslog) Flush()

Flush the current log backend

func (*Syslog) SetWriter

func (l *Syslog) SetWriter(io.Writer)

SetWriter set the io.Writer of current Syslog

func (*Syslog) Writer

func (l *Syslog) Writer() io.Writer

Writer returns the io.Writer of current Syslog

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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