go的原生log模块,功能稍简单。比如不支持自定义输出级别。只有默认的三个可用。println,Fatal,Panic等
以下对go的原生log模块做的一个简易封装:
package main import ( "fmt" "io/ioutil" "log" "mime/multipart" "os" "path" ) // Level These are the integer logging levels used by the logger type Level int // Comment const ( DEBUG Level = iota INFO WARNING ERROR FATAL ) var ( logPrefix = "" levelFlags = []string{"DEBG", "INFO", "WARN", "ERRO", "FATL"} logger *log.Logger loggerf *log.Logger // curLevel ... curLevel Level // logfile *os.File ) func init() { curLevel = DEBUG logger = log.New(os.Stdout, "[default] ", log.LstdFlags) logger.SetFlags(log.Ldate | log.Lmicroseconds | log.Llongfile) } // Println .. func Println(l *log.Logger, v ...interface{}) { if l != nil { l.Output(3, fmt.Sprintln(v...)) } } // Fatalln is equivalent to l.Println() followed by a call to os.Exit(1). func Fatalln(l *log.Logger, v ...interface{}) { if l != nil { l.Output(3, fmt.Sprintln(v...)) os.Exit(1) } } // Debug ... func Debug(v ...interface{}) { setPrefix(DEBUG) if DEBUG >= curLevel { Println(logger, v) Println(loggerf, v) } } // Info ... func Info(v ...interface{}) { setPrefix(INFO) if INFO >= curLevel { Println(logger, v) Println(loggerf, v) } } // Warn ... func Warn(v ...interface{}) { setPrefix(WARNING) if WARNING >= curLevel { Println(logger, v) Println(loggerf, v) } } // Error Warn func Error(v ...interface{}) { setPrefix(ERROR) if ERROR >= curLevel { Println(logger, v) Println(loggerf, v) } } // Fatal ... func Fatal(v ...interface{}) { setPrefix(FATAL) if FATAL >= curLevel { Fatalln(logger, v) Fatalln(loggerf, v) } } func setPrefix(level Level) { logPrefix = fmt.Sprintf("[%s] ", levelFlags[level]) logger.SetPrefix(logPrefix) if loggerf != nil { loggerf.SetPrefix(logPrefix) } } // Config .. func Config(level Level, lfile *os.File) { curLevel = level loggerf = log.New(lfile, "[default] ", log.LstdFlags) loggerf.SetFlags(log.Ldate | log.Lmicroseconds | log.Llongfile) } func main() { lgfile, err := MustOpen("log.txt", "mylog1/") if err != nil { Error("Failed to open log file:" + err.Error()) } Config(DEBUG, lgfile) Debug("message") Info("message") Warn("message") Error("message") }
要支持文件,则再加上以下几个操作文件和目录的:
func GetSize(f multipart.File) (int, error) { content, err := ioutil.ReadAll(f) return len(content), err } func GetExt(fileName string) string { return path.Ext(fileName) } func CheckNotExist(src string) bool { _, err := os.Stat(src) return os.IsNotExist(err) } func CheckPermission(src string) bool { _, err := os.Stat(src) return os.IsPermission(err) } func IsNotExistMkDir(src string) error { if notExist := CheckNotExist(src); notExist == true { if err := MkDir(src); err != nil { return err } } return nil } func MkDir(src string) error { err := os.MkdirAll(src, os.ModePerm) if err != nil { return err } return nil } func Open(name string, flag int, perm os.FileMode) (*os.File, error) { f, err := os.OpenFile(name, flag, perm) if err != nil { return nil, err } return f, nil } func MustOpen(fileName, filePath string) (*os.File, error) { dir, err := os.Getwd() if err != nil { return nil, fmt.Errorf("os.Getwd err: %v", err) } src := dir + "/" + filePath perm := CheckPermission(src) if perm == true { return nil, fmt.Errorf("file.CheckPermission Permission denied src: %s", src) } err = IsNotExistMkDir(src) if err != nil { return nil, fmt.Errorf("file.IsNotExistMkDir src: %s, err: %v", src, err) } f, err := Open(src+fileName, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644) if err != nil { return nil, fmt.Errorf("Fail to OpenFile :%v", err) } return f, nil }