Created
May 17, 2012 13:29
-
-
Save alphazero/2718939 to your computer and use it in GitHub Desktop.
yal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a very thin convenience wrapper over go's log package | |
// that also alters the semantics of go log's Error and Fatal: | |
// none of the functions of this package will ever panic() or | |
// call os.Exit(). | |
// | |
// Log levels are prefixed to the user log data for each | |
// eponymous function i.e. logger.Error(..) will emit a log | |
// message that begins (after prefix) with ERROR - .. | |
// | |
// Package also opts for logger.Lmicroseconds prefix in addition | |
// to the default Prefix(). | |
package log | |
import ( | |
"fmt" | |
"io" | |
stdlib "log" | |
) | |
const ( | |
delim = " - " | |
FATAL = "[FATAL]" | |
ERROR = "[ERROR]" | |
WARN = "[WARN ]" | |
DEBUG = "[DEBUG]" | |
INFO = "[INFO ]" | |
levlen = len(INFO) | |
len2 = len(delim) + levlen | |
len3 = 2*len(delim) + levlen | |
) | |
var levels = []string{FATAL, ERROR, WARN, DEBUG, INFO} | |
type SysLog struct { | |
logger *stdlib.Logger | |
} | |
func NewSysLog(out io.Writer, prefix string, flag int) *SysLog { | |
return &SysLog{stdlib.New(out, prefix, flag)} | |
} | |
// NOTE - the semantics here are different from go's logger.Fatal | |
// It will neither panic nor exit | |
func (sl *SysLog) Fatal(meta string, e error) { | |
sl.logger.Println(join3(FATAL, meta, e.Error())) | |
} | |
// NOTE - the semantics here are different from go's logger.Fatal | |
// It will neither panic nor exit | |
func (sl *SysLog) Fatalf(format string, v ...interface{}) { | |
sl.logger.Println(join2(FATAL, fmt.Sprintf(format, v...))) | |
} | |
func (sl *SysLog) Error(meta string, e error) { | |
sl.logger.Println(join3(ERROR, meta, e.Error())) | |
} | |
func (sl *SysLog) Errorf(format string, v ...interface{}) { | |
sl.logger.Println(join2(ERROR, fmt.Sprintf(format, v...))) | |
} | |
func (sl *SysLog) Debug(msg string) { | |
sl.logger.Println(join2(DEBUG, msg)) | |
} | |
func (sl *SysLog) Debugf(format string, v ...interface{}) { | |
sl.logger.Println(join2(DEBUG, fmt.Sprintf(format, v...))) | |
} | |
func (sl *SysLog) Warn(msg string) { | |
sl.logger.Println(join2(WARN, msg)) | |
} | |
func (sl *SysLog) Warnf(format string, v ...interface{}) { | |
sl.logger.Println(join2(WARN, fmt.Sprintf(format, v...))) | |
} | |
func (sl *SysLog) Info(msg string) { | |
sl.logger.Println(join2(INFO, msg)) | |
} | |
func (sl *SysLog) Infof(format string, v ...interface{}) { | |
sl.logger.Println(join2(INFO, fmt.Sprintf(format, v...))) | |
} | |
func join2(level, msg string) string { | |
n := len(msg) + len2 | |
j := make([]byte, n) | |
o := copy(j, level) | |
o += copy(j[o:], delim) | |
copy(j[o:], msg) | |
return string(j) | |
} | |
func join3(level, meta, msg string) string { | |
n := len(meta) + len(msg) + len3 | |
j := make([]byte, n) | |
o := copy(j, level) | |
o += copy(j[o:], delim) | |
o += copy(j[o:], meta) | |
o += copy(j[o:], delim) | |
copy(j[o:], msg) | |
return string(j) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment