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 officiallog
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 ¶
- Constants
- func Debug(v ...interface{})
- func Debugf(format string, v ...interface{})
- func Debugln(v ...interface{})
- func Error(v ...interface{})
- func Errorf(format string, v ...interface{})
- func Errorln(v ...interface{})
- func Fatal(v ...interface{})
- func Fatalf(format string, v ...interface{})
- func Fatalln(v ...interface{})
- func Flags() int
- func Flush()
- func Info(v ...interface{})
- func Infof(format string, v ...interface{})
- func Infoln(v ...interface{})
- func Name() string
- func Prefix() string
- func Print(v ...interface{})
- func Printf(format string, v ...interface{})
- func Println(v ...interface{})
- func SetFlags(flag int)
- func SetLogLevel(level Level)
- func SetName(name string)
- func SetOutput(w io.Writer)
- func SetPrefix(prefix string)
- func Warn(v ...interface{})
- func Warnf(format string, v ...interface{})
- func Warnln(v ...interface{})
- func Writer() io.Writer
- type AsyncLog
- type Backend
- type ByteSize
- type CompressMethod
- type Handler
- type Level
- type Logger
- func (l *Logger) Debug(v ...interface{})
- func (l *Logger) Debugf(format string, v ...interface{})
- func (l *Logger) Debugln(v ...interface{})
- func (l *Logger) Error(v ...interface{})
- func (l *Logger) Errorf(format string, v ...interface{})
- func (l *Logger) Errorln(v ...interface{})
- func (l *Logger) Fatal(v ...interface{})
- func (l *Logger) Fatalf(format string, v ...interface{})
- func (l *Logger) Fatalln(v ...interface{})
- func (l *Logger) Flags() int
- func (l *Logger) Flush()
- func (l *Logger) Info(v ...interface{})
- func (l *Logger) Infof(format string, v ...interface{})
- func (l *Logger) Infoln(v ...interface{})
- func (l *Logger) Level() Level
- func (l *Logger) Name() string
- func (l *Logger) Output(calldepth int, s string) error
- func (l *Logger) Prefix() string
- func (l *Logger) Print(v ...interface{})
- func (l *Logger) Printf(format string, v ...interface{})
- func (l *Logger) Println(v ...interface{})
- func (l *Logger) SetFlags(flag int)
- func (l *Logger) SetLogLevel(level Level)
- func (l *Logger) SetName(name string)
- func (l *Logger) SetOutput(w io.Writer)
- func (l *Logger) SetPrefix(prefix string)
- func (l *Logger) Warn(v ...interface{})
- func (l *Logger) Warnf(format string, v ...interface{})
- func (l *Logger) Warnln(v ...interface{})
- func (l *Logger) Writer() io.Writer
- type Record
- type RotateLogger
- type SyncLog
- type Syslog
Examples ¶
Constants ¶
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 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 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.
Types ¶
type AsyncLog ¶
type AsyncLog struct {
// contains filtered or unexported fields
}
AsyncLog an async logger
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 ¶
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 ¶
NewSyncBackend create a new sync 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 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 ¶
Output: logger: example_test.go:18: Hello, log file!
func New ¶
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 ¶
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 (*Logger) Debugln ¶
func (l *Logger) Debugln(v ...interface{})
Debugln prints debug log with newline.
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) Fatalln ¶
func (l *Logger) Fatalln(v ...interface{})
Fatalln prints fatal log with newline and exit current process.
func (*Logger) Infoln ¶
func (l *Logger) Infoln(v ...interface{})
Infoln prints info log with newline.
func (*Logger) Output ¶
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 ¶
Output: INFO: example_test.go:31: Hello world
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 ¶
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) SetLogLevel ¶
SetLogLevel update the logger's level
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) 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