logs.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. package logx
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "os"
  7. "path"
  8. "runtime/debug"
  9. "sync"
  10. "sync/atomic"
  11. "time"
  12. "github.com/zeromicro/go-zero/core/sysx"
  13. )
  14. const callerDepth = 4
  15. var (
  16. timeFormat = "2006-01-02T15:04:05.000Z07:00"
  17. logLevel uint32
  18. encoding uint32 = jsonEncodingType
  19. // use uint32 for atomic operations
  20. disableLog uint32
  21. disableStat uint32
  22. options logOptions
  23. writer = new(atomicWriter)
  24. setupOnce sync.Once
  25. )
  26. type (
  27. // LogField is a key-value pair that will be added to the log entry.
  28. LogField struct {
  29. Key string
  30. Value interface{}
  31. }
  32. // LogOption defines the method to customize the logging.
  33. LogOption func(options *logOptions)
  34. logEntry map[string]interface{}
  35. logOptions struct {
  36. gzipEnabled bool
  37. logStackCooldownMills int
  38. keepDays int
  39. maxBackups int
  40. maxSize int
  41. rotationRule string
  42. }
  43. )
  44. // Alert alerts v in alert level, and the message is written to error log.
  45. func Alert(v string) {
  46. getWriter().Alert(v)
  47. }
  48. // Close closes the logging.
  49. func Close() error {
  50. if w := writer.Swap(nil); w != nil {
  51. return w.(io.Closer).Close()
  52. }
  53. return nil
  54. }
  55. // Disable disables the logging.
  56. func Disable() {
  57. atomic.StoreUint32(&disableLog, 1)
  58. writer.Store(nopWriter{})
  59. }
  60. // DisableStat disables the stat logs.
  61. func DisableStat() {
  62. atomic.StoreUint32(&disableStat, 1)
  63. }
  64. // Error writes v into error log.
  65. func Error(v ...interface{}) {
  66. writeError(fmt.Sprint(v...))
  67. }
  68. // Errorf writes v with format into error log.
  69. func Errorf(format string, v ...interface{}) {
  70. writeError(fmt.Errorf(format, v...).Error())
  71. }
  72. // ErrorStack writes v along with call stack into error log.
  73. func ErrorStack(v ...interface{}) {
  74. // there is newline in stack string
  75. writeStack(fmt.Sprint(v...))
  76. }
  77. // ErrorStackf writes v along with call stack in format into error log.
  78. func ErrorStackf(format string, v ...interface{}) {
  79. // there is newline in stack string
  80. writeStack(fmt.Sprintf(format, v...))
  81. }
  82. // Errorv writes v into error log with json content.
  83. // No call stack attached, because not elegant to pack the messages.
  84. func Errorv(v interface{}) {
  85. writeError(v)
  86. }
  87. // Errorw writes msg along with fields into error log.
  88. func Errorw(msg string, fields ...LogField) {
  89. writeError(msg, fields...)
  90. }
  91. // Field returns a LogField for the given key and value.
  92. func Field(key string, value interface{}) LogField {
  93. switch val := value.(type) {
  94. case error:
  95. return LogField{Key: key, Value: val.Error()}
  96. case []error:
  97. var errs []string
  98. for _, err := range val {
  99. errs = append(errs, err.Error())
  100. }
  101. return LogField{Key: key, Value: errs}
  102. case time.Duration:
  103. return LogField{Key: key, Value: fmt.Sprint(val)}
  104. case []time.Duration:
  105. var durs []string
  106. for _, dur := range val {
  107. durs = append(durs, fmt.Sprint(dur))
  108. }
  109. return LogField{Key: key, Value: durs}
  110. case []time.Time:
  111. var times []string
  112. for _, t := range val {
  113. times = append(times, fmt.Sprint(t))
  114. }
  115. return LogField{Key: key, Value: times}
  116. case fmt.Stringer:
  117. return LogField{Key: key, Value: val.String()}
  118. case []fmt.Stringer:
  119. var strs []string
  120. for _, str := range val {
  121. strs = append(strs, str.String())
  122. }
  123. return LogField{Key: key, Value: strs}
  124. default:
  125. return LogField{Key: key, Value: val}
  126. }
  127. }
  128. // Info writes v into access log.
  129. func Info(v ...interface{}) {
  130. writeInfo(fmt.Sprint(v...))
  131. }
  132. // Infof writes v with format into access log.
  133. func Infof(format string, v ...interface{}) {
  134. writeInfo(fmt.Sprintf(format, v...))
  135. }
  136. // Infov writes v into access log with json content.
  137. func Infov(v interface{}) {
  138. writeInfo(v)
  139. }
  140. // Infow writes msg along with fields into access log.
  141. func Infow(msg string, fields ...LogField) {
  142. writeInfo(msg, fields...)
  143. }
  144. // Must checks if err is nil, otherwise logs the error and exits.
  145. func Must(err error) {
  146. if err == nil {
  147. return
  148. }
  149. msg := err.Error()
  150. log.Print(msg)
  151. getWriter().Severe(msg)
  152. os.Exit(1)
  153. }
  154. // MustSetup sets up logging with given config c. It exits on error.
  155. func MustSetup(c LogConf) {
  156. Must(SetUp(c))
  157. }
  158. // Reset clears the writer and resets the log level.
  159. func Reset() Writer {
  160. return writer.Swap(nil)
  161. }
  162. // SetLevel sets the logging level. It can be used to suppress some logs.
  163. func SetLevel(level uint32) {
  164. atomic.StoreUint32(&logLevel, level)
  165. }
  166. // SetWriter sets the logging writer. It can be used to customize the logging.
  167. func SetWriter(w Writer) {
  168. if atomic.LoadUint32(&disableLog) == 0 {
  169. writer.Store(w)
  170. }
  171. }
  172. // SetUp sets up the logx. If already set up, just return nil.
  173. // we allow SetUp to be called multiple times, because for example
  174. // we need to allow different service frameworks to initialize logx respectively.
  175. func SetUp(c LogConf) (err error) {
  176. // Just ignore the subsequent SetUp calls.
  177. // Because multiple services in one process might call SetUp respectively.
  178. // Need to wait for the first caller to complete the execution.
  179. setupOnce.Do(func() {
  180. setupLogLevel(c)
  181. if len(c.TimeFormat) > 0 {
  182. timeFormat = c.TimeFormat
  183. }
  184. switch c.Encoding {
  185. case plainEncoding:
  186. atomic.StoreUint32(&encoding, plainEncodingType)
  187. default:
  188. atomic.StoreUint32(&encoding, jsonEncodingType)
  189. }
  190. switch c.Mode {
  191. case fileMode:
  192. err = setupWithFiles(c)
  193. case volumeMode:
  194. err = setupWithVolume(c)
  195. default:
  196. setupWithConsole()
  197. }
  198. })
  199. return
  200. }
  201. // Severe writes v into severe log.
  202. func Severe(v ...interface{}) {
  203. writeSevere(fmt.Sprint(v...))
  204. }
  205. // Severef writes v with format into severe log.
  206. func Severef(format string, v ...interface{}) {
  207. writeSevere(fmt.Sprintf(format, v...))
  208. }
  209. // Slow writes v into slow log.
  210. func Slow(v ...interface{}) {
  211. writeSlow(fmt.Sprint(v...))
  212. }
  213. // Slowf writes v with format into slow log.
  214. func Slowf(format string, v ...interface{}) {
  215. writeSlow(fmt.Sprintf(format, v...))
  216. }
  217. // Slowv writes v into slow log with json content.
  218. func Slowv(v interface{}) {
  219. writeSlow(v)
  220. }
  221. // Sloww writes msg along with fields into slow log.
  222. func Sloww(msg string, fields ...LogField) {
  223. writeSlow(msg, fields...)
  224. }
  225. // Stat writes v into stat log.
  226. func Stat(v ...interface{}) {
  227. writeStat(fmt.Sprint(v...))
  228. }
  229. // Statf writes v with format into stat log.
  230. func Statf(format string, v ...interface{}) {
  231. writeStat(fmt.Sprintf(format, v...))
  232. }
  233. // WithCooldownMillis customizes logging on writing call stack interval.
  234. func WithCooldownMillis(millis int) LogOption {
  235. return func(opts *logOptions) {
  236. opts.logStackCooldownMills = millis
  237. }
  238. }
  239. // WithKeepDays customizes logging to keep logs with days.
  240. func WithKeepDays(days int) LogOption {
  241. return func(opts *logOptions) {
  242. opts.keepDays = days
  243. }
  244. }
  245. // WithGzip customizes logging to automatically gzip the log files.
  246. func WithGzip() LogOption {
  247. return func(opts *logOptions) {
  248. opts.gzipEnabled = true
  249. }
  250. }
  251. // WithMaxBackups customizes how many log files backups will be kept.
  252. func WithMaxBackups(count int) LogOption {
  253. return func(opts *logOptions) {
  254. opts.maxBackups = count
  255. }
  256. }
  257. // WithMaxSize customizes how much space the writing log file can take up.
  258. func WithMaxSize(size int) LogOption {
  259. return func(opts *logOptions) {
  260. opts.maxSize = size
  261. }
  262. }
  263. // WithRotation customizes which log rotation rule to use.
  264. func WithRotation(r string) LogOption {
  265. return func(opts *logOptions) {
  266. opts.rotationRule = r
  267. }
  268. }
  269. func addCaller(fields ...LogField) []LogField {
  270. return append(fields, Field(callerKey, getCaller(callerDepth)))
  271. }
  272. func createOutput(path string) (io.WriteCloser, error) {
  273. if len(path) == 0 {
  274. return nil, ErrLogPathNotSet
  275. }
  276. switch options.rotationRule {
  277. case sizeRotationRule:
  278. return NewLogger(path, NewSizeLimitRotateRule(path, backupFileDelimiter, options.keepDays,
  279. options.maxSize, options.maxBackups, options.gzipEnabled), options.gzipEnabled)
  280. default:
  281. return NewLogger(path, DefaultRotateRule(path, backupFileDelimiter, options.keepDays,
  282. options.gzipEnabled), options.gzipEnabled)
  283. }
  284. }
  285. func getWriter() Writer {
  286. w := writer.Load()
  287. if w == nil {
  288. w = writer.StoreIfNil(newConsoleWriter())
  289. }
  290. return w
  291. }
  292. func handleOptions(opts []LogOption) {
  293. for _, opt := range opts {
  294. opt(&options)
  295. }
  296. }
  297. func setupLogLevel(c LogConf) {
  298. switch c.Level {
  299. case levelInfo:
  300. SetLevel(InfoLevel)
  301. case levelError:
  302. SetLevel(ErrorLevel)
  303. case levelSevere:
  304. SetLevel(SevereLevel)
  305. }
  306. }
  307. func setupWithConsole() {
  308. SetWriter(newConsoleWriter())
  309. }
  310. func setupWithFiles(c LogConf) error {
  311. w, err := newFileWriter(c)
  312. if err != nil {
  313. return err
  314. }
  315. SetWriter(w)
  316. return nil
  317. }
  318. func setupWithVolume(c LogConf) error {
  319. if len(c.ServiceName) == 0 {
  320. return ErrLogServiceNameNotSet
  321. }
  322. c.Path = path.Join(c.Path, c.ServiceName, sysx.Hostname())
  323. return setupWithFiles(c)
  324. }
  325. func shallLog(level uint32) bool {
  326. return atomic.LoadUint32(&logLevel) <= level
  327. }
  328. func shallLogStat() bool {
  329. return atomic.LoadUint32(&disableStat) == 0
  330. }
  331. func writeError(val interface{}, fields ...LogField) {
  332. if shallLog(ErrorLevel) {
  333. getWriter().Error(val, addCaller(fields...)...)
  334. }
  335. }
  336. func writeInfo(val interface{}, fields ...LogField) {
  337. if shallLog(InfoLevel) {
  338. getWriter().Info(val, addCaller(fields...)...)
  339. }
  340. }
  341. func writeSevere(msg string) {
  342. if shallLog(SevereLevel) {
  343. getWriter().Severe(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
  344. }
  345. }
  346. func writeSlow(val interface{}, fields ...LogField) {
  347. if shallLog(ErrorLevel) {
  348. getWriter().Slow(val, addCaller(fields...)...)
  349. }
  350. }
  351. func writeStack(msg string) {
  352. if shallLog(ErrorLevel) {
  353. getWriter().Stack(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
  354. }
  355. }
  356. func writeStat(msg string) {
  357. if shallLogStat() && shallLog(InfoLevel) {
  358. getWriter().Stat(msg, addCaller()...)
  359. }
  360. }