github.com/zhongdalu/gf@v1.0.0/g/os/glog/glog.go (about) 1 // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 // Package glog implements powerful and easy-to-use levelled logging functionality. 8 package glog 9 10 import ( 11 "io" 12 13 "github.com/zhongdalu/gf/g/internal/cmdenv" 14 "github.com/zhongdalu/gf/g/os/grpool" 15 ) 16 17 const ( 18 LEVEL_ALL = LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT 19 LEVEL_DEV = LEVEL_ALL 20 LEVEL_PROD = LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT 21 LEVEL_DEBU = 1 << iota 22 LEVEL_INFO 23 LEVEL_NOTI 24 LEVEL_WARN 25 LEVEL_ERRO 26 LEVEL_CRIT 27 ) 28 29 var ( 30 // Default logger object, for package method usage 31 logger = New() 32 // Goroutine pool for async logging output. 33 asyncPool = grpool.New(1) 34 ) 35 36 func init() { 37 SetDebug(cmdenv.Get("gf.glog.debug", true).Bool()) 38 } 39 40 // SetPath sets the directory path for file logging. 41 func SetPath(path string) error { 42 return logger.SetPath(path) 43 } 44 45 // GetPath returns the logging directory path for file logging. 46 // It returns empty string if no directory path set. 47 func GetPath() string { 48 return logger.GetPath() 49 } 50 51 // SetFile sets the file name <pattern> for file logging. 52 // Datetime pattern can be used in <pattern>, eg: access-{Ymd}.log. 53 // The default file name pattern is: Y-m-d.log, eg: 2018-01-01.log 54 func SetFile(pattern string) { 55 logger.SetFile(pattern) 56 } 57 58 // SetLevel sets the default logging level. 59 func SetLevel(level int) { 60 logger.SetLevel(level) 61 } 62 63 // GetLevel returns the default logging level value. 64 func GetLevel() int { 65 return logger.GetLevel() 66 } 67 68 // SetWriter sets the customized logging <writer> for logging. 69 // The <writer> object should implements the io.Writer interface. 70 // Developer can use customized logging <writer> to redirect logging output to another service, 71 // eg: kafka, mysql, mongodb, etc. 72 func SetWriter(writer io.Writer) { 73 logger.SetWriter(writer) 74 } 75 76 // GetWriter returns the customized writer object, which implements the io.Writer interface. 77 // It returns nil if no customized writer set. 78 func GetWriter() io.Writer { 79 return logger.GetWriter() 80 } 81 82 // SetDebug enables/disables the debug level for default logger. 83 // The debug level is enbaled in default. 84 func SetDebug(debug bool) { 85 logger.SetDebug(debug) 86 } 87 88 // SetAsync enables/disables async logging output feature for default logger. 89 func SetAsync(enabled bool) { 90 logger.SetAsync(enabled) 91 } 92 93 // SetStdoutPrint sets whether ouptput the logging contents to stdout, which is true in default. 94 func SetStdoutPrint(enabled bool) { 95 logger.SetStdoutPrint(enabled) 96 } 97 98 // SetHeaderPrint sets whether output header of the logging contents, which is true in default. 99 func SetHeaderPrint(enabled bool) { 100 logger.SetHeaderPrint(enabled) 101 } 102 103 // SetPrefix sets prefix string for every logging content. 104 // Prefix is part of header, which means if header output is shut, no prefix will be output. 105 func SetPrefix(prefix string) { 106 logger.SetPrefix(prefix) 107 } 108 109 // SetFlags sets extra flags for logging output features. 110 func SetFlags(flags int) { 111 logger.SetFlags(flags) 112 } 113 114 // GetFlags returns the flags of logger. 115 func GetFlags() int { 116 return logger.GetFlags() 117 } 118 119 // PrintStack prints the caller stack, 120 // the optional parameter <skip> specify the skipped stack offset from the end point. 121 func PrintStack(skip ...int) { 122 logger.PrintStack(skip...) 123 } 124 125 // GetStack returns the caller stack content, 126 // the optional parameter <skip> specify the skipped stack offset from the end point. 127 func GetStack(skip ...int) string { 128 return logger.GetStack(skip...) 129 } 130 131 // SetStack enables/disables the stack feature in failure logging outputs. 132 func SetStack(enabled bool) { 133 logger.SetStack(enabled) 134 }