From 36cf27f32416d370941596924dc9be49f5ab04ec Mon Sep 17 00:00:00 2001 From: Guoqiang Chen Date: Sat, 19 May 2018 06:45:42 +0000 Subject: [PATCH] Update comments on public structs and methods --- formatter.go | 1 + formatters/util_fileline.go | 1 + formatters/util_terminal.go | 1 + log.go | 58 +++++++++++++++---------------- logger.go | 32 +++++++++++++++++ writers/always_new_file_writer.go | 1 + writers/daily_file_writer.go | 1 + writers/fixed_size_file_writer.go | 1 + 8 files changed, 67 insertions(+), 29 deletions(-) diff --git a/formatter.go b/formatter.go index 2d9ebba..f42fe86 100644 --- a/formatter.go +++ b/formatter.go @@ -14,6 +14,7 @@ type Formatter interface { type simpleFormatter struct { } +// Format implements log.Formatter func (f simpleFormatter) Format(level Level, msg string, logger *Logger) []byte { time := time.Now().Format("15:04:05.000") return []byte(fmt.Sprintf("%s %s %s\n", time, level.String(), msg)) diff --git a/formatters/util_fileline.go b/formatters/util_fileline.go index 9288439..3f2b3c3 100644 --- a/formatters/util_fileline.go +++ b/formatters/util_fileline.go @@ -5,6 +5,7 @@ import ( "strings" ) +// FilelineCaller returns file and line for caller func FilelineCaller(skip int) (file string, line int) { for i := 0; i < 10; i++ { _, file, line, ok := runtime.Caller(skip + i) diff --git a/formatters/util_terminal.go b/formatters/util_terminal.go index 2008e22..ab402b8 100644 --- a/formatters/util_terminal.go +++ b/formatters/util_terminal.go @@ -7,6 +7,7 @@ import ( "golang.org/x/crypto/ssh/terminal" ) +// IsTerminal returns whether is a valid tty for io.Writer func IsTerminal(w io.Writer) bool { switch v := w.(type) { case *os.File: diff --git a/log.go b/log.go index 004e5ad..ac7d8fd 100644 --- a/log.go +++ b/log.go @@ -3,147 +3,147 @@ package log // Default is a default Logger instance var Default = New() -// Indicate whether output debug message +// IsDebugEnabled indicates whether output message func IsDebugEnabled() bool { return Default.IsDebugEnabled() } -// Indicate whether output info message +// IsInfoEnabled indicates whether output message func IsInfoEnabled() bool { return Default.IsInfoEnabled() } -// Indicate whether output info message +// IsPrintEnabled indicates whether output message func IsPrintEnabled() bool { return Default.IsPrintEnabled() } -// Indicate whether output warning message +// IsWarnEnabled indicates whether output message func IsWarnEnabled() bool { return Default.IsWarnEnabled() } -// Indicate whether output error message +// IsErrorEnabled indicates whether output message func IsErrorEnabled() bool { return Default.IsErrorEnabled() } -// Indicate whether output panic message +// IsPanicEnabled indicates whether output message func IsPanicEnabled() bool { return Default.IsPanicEnabled() } -// Indicate whether output fatal message +// IsFatalEnabled indicates whether output message func IsFatalEnabled() bool { return Default.IsFatalEnabled() } -// Indicate whether output is off +// IsDisabled indicates whether output message func IsDisabled() bool { return Default.IsDisabled() } -// Output a debug message +// Debug outputs message, Arguments are handled by fmt.Sprint func Debug(obj ...interface{}) { Default.Debug(obj...) } -// Output an info message +// Info outputs message, Arguments are handled by fmt.Sprint func Info(obj ...interface{}) { Default.Info(obj...) } -// Output an info message +// Print outputs message, Arguments are handled by fmt.Sprint func Print(obj ...interface{}) { Default.Print(obj...) } -// Output a warning message +// Warn outputs message, Arguments are handled by fmt.Sprint func Warn(obj ...interface{}) { Default.Warn(obj...) } -// Output an error message +// Error outputs message, Arguments are handled by fmt.Sprint func Error(obj ...interface{}) { Default.Error(obj...) } -// Output a panic message with full stack +// Panic outputs message, and followed by a call to panic() Arguments are handled by fmt.Sprint func Panic(obj ...interface{}) { Default.Panic(obj...) } -// Output a fatal message with full stack +// Fatal outputs message, and followed by a call to os.Exit(1) Arguments are handled by fmt.Sprint func Fatal(obj ...interface{}) { Default.Fatal(obj...) } -// Output a debug message +// Debugln outputs message, Arguments are handled by fmt.Sprintln func Debugln(obj ...interface{}) { Default.Debugln(obj...) } -// Output an info message +// Infoln outputs message, Arguments are handled by fmt.Sprintln func Infoln(obj ...interface{}) { Default.Infoln(obj...) } -// Output an info message +// Println outputs message, Arguments are handled by fmt.Sprintln func Println(obj ...interface{}) { Default.Println(obj...) } -// Output a warning message +// Warnln outputs message, Arguments are handled by fmt.Sprintln func Warnln(obj ...interface{}) { Default.Warnln(obj...) } -// Output an error message +// Errorln outputs message, Arguments are handled by fmt.Sprintln func Errorln(obj ...interface{}) { Default.Errorln(obj...) } -// Output a panic message with full stack +// Panicln outputs message and followed by a call to panic(), Arguments are handled by fmt.Sprintln func Panicln(obj ...interface{}) { Default.Panicln(obj...) } -// Output a fatal message with full stack +// Fatalln outputs message and followed by a call to os.Exit(1), Arguments are handled by fmt.Sprintln func Fatalln(obj ...interface{}) { Default.Fatalln(obj...) } -// Output a debug message +// Debugf outputs message, Arguments are handled by fmt.Sprintf func Debugf(msg string, args ...interface{}) { Default.Debugf(msg, args...) } -// Output an info message +// Infof outputs message, Arguments are handled by fmt.Sprintf func Infof(msg string, args ...interface{}) { Default.Infof(msg, args...) } -// Output an info message +// Printf outputs message, Arguments are handled by fmt.Sprintf func Printf(msg string, args ...interface{}) { Default.Printf(msg, args...) } -// Output a warning message +// Warnf outputs message, Arguments are handled by fmt.Sprintf func Warnf(msg string, args ...interface{}) { Default.Warnf(msg, args...) } -// Output an error message +// Errorf outputs message, Arguments are handled by fmt.Sprintf func Errorf(msg string, args ...interface{}) { Default.Errorf(msg, args...) } -// Output a panic message with full stack +// Panicf outputs message and followed by a call to panic(), Arguments are handled by fmt.Sprintf func Panicf(msg string, args ...interface{}) { Default.Panicf(msg, args...) } -// Output a fatal message with full stack +// Fatalf outputs message and followed by a call to os.Exit(1), Arguments are handled by fmt.Sprintf func Fatalf(msg string, args ...interface{}) { Default.Fatalf(msg, args...) } diff --git a/logger.go b/logger.go index 5ef292b..11efda4 100644 --- a/logger.go +++ b/logger.go @@ -7,8 +7,10 @@ import ( "sync" ) +// Exit is equals os.Exit var Exit = os.Exit +// Logger is represents an active logging object type Logger struct { m sync.Mutex Level Level @@ -16,6 +18,7 @@ type Logger struct { Out io.Writer } +// New creates a new Logger func New() *Logger { return &Logger{ Level: INFO, @@ -24,68 +27,82 @@ func New() *Logger { } } +// IsDebugEnabled indicates whether output message func (l *Logger) IsDebugEnabled() bool { return l.Level >= DEBUG } +// IsInfoEnabled indicates whether output message func (l *Logger) IsInfoEnabled() bool { return l.Level >= INFO } +// IsPrintEnabled indicates whether output message func (l *Logger) IsPrintEnabled() bool { return l.Level > OFF } +// IsWarnEnabled indicates whether output message func (l *Logger) IsWarnEnabled() bool { return l.Level >= WARN } +// IsErrorEnabled indicates whether output message func (l *Logger) IsErrorEnabled() bool { return l.Level >= ERROR } +// IsPanicEnabled indicates whether output message func (l *Logger) IsPanicEnabled() bool { return l.Level >= PANIC } +// IsFatalEnabled indicates whether output message func (l *Logger) IsFatalEnabled() bool { return l.Level >= FATAL } +// IsDisabled indicates whether output message func (l *Logger) IsDisabled() bool { return l.Level <= OFF } +// Debug outputs message, Arguments are handled by fmt.Sprint func (l *Logger) Debug(obj ...interface{}) { if l.Level >= DEBUG { l.log(DEBUG, fmt.Sprint(obj...)) } } +// Info outputs message, Arguments are handled by fmt.Sprint func (l *Logger) Info(obj ...interface{}) { if l.Level >= INFO { l.log(INFO, fmt.Sprint(obj...)) } } +// Print outputs message, Arguments are handled by fmt.Sprint func (l *Logger) Print(obj ...interface{}) { if l.Level != OFF { l.log(INFO, fmt.Sprint(obj...)) } } +// Warn outputs message, Arguments are handled by fmt.Sprint func (l *Logger) Warn(obj ...interface{}) { if l.Level >= WARN { l.log(WARN, fmt.Sprint(obj...)) } } +// Error outputs message, Arguments are handled by fmt.Sprint func (l *Logger) Error(obj ...interface{}) { if l.Level >= ERROR { l.log(ERROR, fmt.Sprint(obj...)) } } +// Panic outputs message, and followed by a call to panic() Arguments are handled by fmt.Sprint func (l *Logger) Panic(obj ...interface{}) { if l.Level >= PANIC { l.log(PANIC, fmt.Sprint(obj...)) @@ -93,6 +110,7 @@ func (l *Logger) Panic(obj ...interface{}) { panic(fmt.Sprint(obj...)) } +// Fatal outputs message and followed by a call to os.Exit(1), Arguments are handled by fmt.Sprint func (l *Logger) Fatal(obj ...interface{}) { if l.Level >= FATAL { l.log(FATAL, fmt.Sprint(obj...)) @@ -100,36 +118,42 @@ func (l *Logger) Fatal(obj ...interface{}) { Exit(1) } +// Debugln outputs message, Arguments are handled by fmt.Sprintln func (l *Logger) Debugln(obj ...interface{}) { if l.Level >= DEBUG { l.log(DEBUG, vsprintln(obj...)) } } +// Infoln outputs message, Arguments are handled by fmt.Sprintln func (l *Logger) Infoln(obj ...interface{}) { if l.Level >= INFO { l.log(INFO, vsprintln(obj...)) } } +// Println outputs message, Arguments are handled by fmt.Sprintln func (l *Logger) Println(obj ...interface{}) { if l.Level != OFF { l.log(INFO, vsprintln(obj...)) } } +// Warnln outputs message, Arguments are handled by fmt.Sprintln func (l *Logger) Warnln(obj ...interface{}) { if l.Level >= WARN { l.log(WARN, vsprintln(obj...)) } } +// Errorln outputs message, Arguments are handled by fmt.Sprintln func (l *Logger) Errorln(obj ...interface{}) { if l.Level >= ERROR { l.log(ERROR, vsprintln(obj...)) } } +// Panicln outputs message and followed by a call to panic(), Arguments are handled by fmt.Sprintln func (l *Logger) Panicln(obj ...interface{}) { if l.Level >= PANIC { l.log(PANIC, vsprintln(obj...)) @@ -137,6 +161,7 @@ func (l *Logger) Panicln(obj ...interface{}) { panic(vsprintln(obj...)) } +// Fatalln outputs message and followed by a call to os.Exit(1), Arguments are handled by fmt.Sprintln func (l *Logger) Fatalln(obj ...interface{}) { if l.Level >= FATAL { l.log(FATAL, vsprintln(obj...)) @@ -144,36 +169,42 @@ func (l *Logger) Fatalln(obj ...interface{}) { Exit(1) } +// Debugf outputs message, Arguments are handles by fmt.Sprintf func (l *Logger) Debugf(msg string, args ...interface{}) { if l.Level >= DEBUG { l.log(DEBUG, fmt.Sprintf(msg, args...)) } } +// Infof outputs message, Arguments are handles by fmt.Sprintf func (l *Logger) Infof(msg string, args ...interface{}) { if l.Level >= INFO { l.log(INFO, fmt.Sprintf(msg, args...)) } } +// Printf outputs message, Arguments are handles by fmt.Sprintf func (l *Logger) Printf(msg string, args ...interface{}) { if l.Level != OFF { l.log(INFO, fmt.Sprintf(msg, args...)) } } +// Warnf outputs message, Arguments are handles by fmt.Sprintf func (l *Logger) Warnf(msg string, args ...interface{}) { if l.Level >= WARN { l.log(WARN, fmt.Sprintf(msg, args...)) } } +// Errorf outputs message, Arguments are handles by fmt.Sprintf func (l *Logger) Errorf(msg string, args ...interface{}) { if l.Level >= ERROR { l.log(ERROR, fmt.Sprintf(msg, args...)) } } +// Panicf outputs message and followed by a call to panic(), Arguments are handles by fmt.Sprintf func (l *Logger) Panicf(msg string, args ...interface{}) { if l.Level >= PANIC { l.log(PANIC, fmt.Sprintf(msg, args...)) @@ -181,6 +212,7 @@ func (l *Logger) Panicf(msg string, args ...interface{}) { panic(fmt.Sprintf(msg, args...)) } +// Fatalf outputs message and followed by a call to os.Exit(1), Arguments are handles by fmt.Sprintf func (l *Logger) Fatalf(msg string, args ...interface{}) { if l.Level >= FATAL { l.log(FATAL, fmt.Sprintf(msg, args...)) diff --git a/writers/always_new_file_writer.go b/writers/always_new_file_writer.go index b3e8d5b..8402538 100644 --- a/writers/always_new_file_writer.go +++ b/writers/always_new_file_writer.go @@ -19,6 +19,7 @@ type AlwaysNewFileWriter struct { file *os.File } +// Write implements io.Writer func (w *AlwaysNewFileWriter) Write(p []byte) (n int, err error) { if w.file == nil { w.openFile() diff --git a/writers/daily_file_writer.go b/writers/daily_file_writer.go index 8347386..72d21ca 100644 --- a/writers/daily_file_writer.go +++ b/writers/daily_file_writer.go @@ -20,6 +20,7 @@ type DailyFileWriter struct { nextDayTime int64 } +// Write implements io.Writer func (w *DailyFileWriter) Write(p []byte) (n int, err error) { now := time.Now() diff --git a/writers/fixed_size_file_writer.go b/writers/fixed_size_file_writer.go index 1070d42..b53fdbf 100644 --- a/writers/fixed_size_file_writer.go +++ b/writers/fixed_size_file_writer.go @@ -17,6 +17,7 @@ type FixedSizeFileWriter struct { currentSize int64 } +// Write implements io.Writer func (w *FixedSizeFileWriter) Write(p []byte) (n int, err error) { if w.file == nil { w.openCurrentFile()