Skip to content

Instantly share code, notes, and snippets.

@budui
Created May 1, 2020 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save budui/12dfa5b9c5f71e7e7b8991cc3815efab to your computer and use it in GitHub Desktop.
Save budui/12dfa5b9c5f71e7e7b8991cc3815efab to your computer and use it in GitHub Desktop.
/*
Package logger add level feature log for *log*.
*/
package logger
import (
"fmt"
"io"
"log"
"os"
)
// 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
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
LstdFlags = Ldate | Ltime // initial values for the standard logger
)
// LogLevel type.
type LogLevel uint32
const (
// FatalLevel should be used in fatal situations, the app will exit.
FatalLevel LogLevel = iota
// ErrorLevel should be used when someone should really look at the error.
ErrorLevel
// InfoLevel should be used during normal operations.
InfoLevel
// DebugLevel should be used only during development.
DebugLevel
)
func (level LogLevel) String() string {
switch level {
case DebugLevel:
return "DEBUG"
case InfoLevel:
return "INFO"
case ErrorLevel:
return "ERROR"
case FatalLevel:
return "FATAL"
default:
return "UNKNOWN"
}
}
// Logger add level for *log.Logger*
type Logger struct {
*log.Logger
level LogLevel
}
// New return a debug logger
func New(out io.Writer, prefix string, flag int, level LogLevel) *Logger {
return &Logger{log.New(out, prefix, flag), level}
}
// Debug calls l.Output to print to the logger
// Arguments are handled in the manner of fmt.Print
func (l *Logger) Debug(v ...interface{}) {
if l.level >= DebugLevel {
l.Output(2, fmt.Sprintf("[%s] %s", DebugLevel, fmt.Sprint(v...)))
}
}
// Debugln calls l.Output to print to the logger
// Arguments are handled in the manner of fmt.Println
func (l *Logger) Debugln(v ...interface{}) {
if l.level >= DebugLevel {
l.Output(2, fmt.Sprintf("[%s] %s", DebugLevel, fmt.Sprintln(v...)))
}
}
// Debugf calls l.Output to print to the logger
// Arguments are handled in the manner of fmt.Printf
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.level >= DebugLevel {
l.Output(2, fmt.Sprintf("[%s] %s", DebugLevel, fmt.Sprintf(format, v...)))
}
}
// Info calls l.Output to print to the logger
// Arguments are handled in the manner of fmt.Print
func (l *Logger) Info(v ...interface{}) {
if l.level >= InfoLevel {
l.Output(2, fmt.Sprintf("[%s] %s", InfoLevel, fmt.Sprint(v...)))
}
}
// Infoln calls l.Output to print to the logger
// Arguments are handled in the manner of fmt.Println
func (l *Logger) Infoln(v ...interface{}) {
if l.level >= InfoLevel {
l.Output(2, fmt.Sprintf("[%s] %s", InfoLevel, fmt.Sprintln(v...)))
}
}
// Infof calls l.Output to print to the logger
// Arguments are handled in the manner of fmt.Printf
func (l *Logger) Infof(format string, v ...interface{}) {
if l.level >= InfoLevel {
l.Output(2, fmt.Sprintf("[%s] %s", InfoLevel, fmt.Sprintf(format, v...)))
}
}
// Error calls l.Output to print to the logger
// Arguments are handled in the manner of fmt.Print
func (l *Logger) Error(v ...interface{}) {
if l.level >= ErrorLevel {
l.Output(2, fmt.Sprintf("[%s] %s", ErrorLevel, fmt.Sprint(v...)))
}
}
// Errorln calls l.Output to print to the logger
// Arguments are handled in the manner of fmt.Println
func (l *Logger) Errorln(v ...interface{}) {
if l.level >= ErrorLevel {
l.Output(2, fmt.Sprintf("[%s] %s", ErrorLevel, fmt.Sprintln(v...)))
}
}
// Errorf calls l.Output to print to the logger
// Arguments are handled in the manner of fmt.Printf
func (l *Logger) Errorf(format string, v ...interface{}) {
if l.level >= ErrorLevel {
l.Output(2, fmt.Sprintf("[%s] %s", ErrorLevel, fmt.Sprintf(format, v...)))
}
}
// Fatal calls l.Output to print to the logger
// Arguments are handled in the manner of fmt.Print
// followed by a call to os.Exit(1)
func (l *Logger) Fatal(v ...interface{}) {
l.Output(2, fmt.Sprintf("[%s] %s", FatalLevel, fmt.Sprint(v...)))
os.Exit(1)
}
// Fatalln calls l.Output to print to the logger
// Arguments are handled in the manner of fmt.Println
// followed by a call to os.Exit(1)
func (l *Logger) Fatalln(v ...interface{}) {
l.Output(2, fmt.Sprintf("[%s] %s", FatalLevel, fmt.Sprintln(v...)))
os.Exit(1)
}
// Fatalf calls l.Output to print to the logger
// Arguments are handled in the manner of fmt.Printf
// followed by a call to os.Exit(1)
func (l *Logger) Fatalf(format string, v ...interface{}) {
l.Output(2, fmt.Sprintf("[%s] %s", FatalLevel, fmt.Sprintf(format, v...)))
os.Exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment