1
0

logs.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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/wuntsong-org/go-zero-plus/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. // maxContentLength is used to truncate the log content, 0 for not truncating.
  20. maxContentLength uint32
  21. // use uint32 for atomic operations
  22. disableLog uint32
  23. disableStat uint32
  24. options logOptions
  25. writer = new(atomicWriter)
  26. setupOnce sync.Once
  27. )
  28. type (
  29. // LogField is a key-value pair that will be added to the log entry.
  30. LogField struct {
  31. Key string
  32. Value any
  33. }
  34. // LogOption defines the method to customize the logging.
  35. LogOption func(options *logOptions)
  36. logEntry map[string]any
  37. logOptions struct {
  38. gzipEnabled bool
  39. logStackCooldownMills int
  40. keepDays int
  41. maxBackups int
  42. maxSize int
  43. rotationRule string
  44. }
  45. )
  46. // Alert alerts v in alert level, and the message is written to error log.
  47. func Alert(v string) {
  48. getWriter().Alert(v)
  49. }
  50. // Close closes the logging.
  51. func Close() error {
  52. if w := writer.Swap(nil); w != nil {
  53. return w.(io.Closer).Close()
  54. }
  55. return nil
  56. }
  57. // Debug writes v into access log.
  58. func Debug(v ...any) {
  59. if shallLog(DebugLevel) {
  60. writeDebug(fmt.Sprint(v...))
  61. }
  62. }
  63. // Debugf writes v with format into access log.
  64. func Debugf(format string, v ...any) {
  65. if shallLog(DebugLevel) {
  66. writeDebug(fmt.Sprintf(format, v...))
  67. }
  68. }
  69. // Debugv writes v into access log with json content.
  70. func Debugv(v any) {
  71. if shallLog(DebugLevel) {
  72. writeDebug(v)
  73. }
  74. }
  75. // Debugw writes msg along with fields into access log.
  76. func Debugw(msg string, fields ...LogField) {
  77. if shallLog(DebugLevel) {
  78. writeDebug(msg, fields...)
  79. }
  80. }
  81. // Disable disables the logging.
  82. func Disable() {
  83. atomic.StoreUint32(&disableLog, 1)
  84. writer.Store(nopWriter{})
  85. }
  86. // DisableStat disables the stat logs.
  87. func DisableStat() {
  88. atomic.StoreUint32(&disableStat, 1)
  89. }
  90. // Error writes v into error log.
  91. func Error(v ...any) {
  92. if shallLog(ErrorLevel) {
  93. writeError(fmt.Sprint(v...))
  94. }
  95. }
  96. // Errorf writes v with format into error log.
  97. func Errorf(format string, v ...any) {
  98. if shallLog(ErrorLevel) {
  99. writeError(fmt.Errorf(format, v...).Error())
  100. }
  101. }
  102. // ErrorStack writes v along with call stack into error log.
  103. func ErrorStack(v ...any) {
  104. if shallLog(ErrorLevel) {
  105. // there is newline in stack string
  106. writeStack(fmt.Sprint(v...))
  107. }
  108. }
  109. // ErrorStackf writes v along with call stack in format into error log.
  110. func ErrorStackf(format string, v ...any) {
  111. if shallLog(ErrorLevel) {
  112. // there is newline in stack string
  113. writeStack(fmt.Sprintf(format, v...))
  114. }
  115. }
  116. // Errorv writes v into error log with json content.
  117. // No call stack attached, because not elegant to pack the messages.
  118. func Errorv(v any) {
  119. if shallLog(ErrorLevel) {
  120. writeError(v)
  121. }
  122. }
  123. // Errorw writes msg along with fields into error log.
  124. func Errorw(msg string, fields ...LogField) {
  125. if shallLog(ErrorLevel) {
  126. writeError(msg, fields...)
  127. }
  128. }
  129. // Field returns a LogField for the given key and value.
  130. func Field(key string, value any) LogField {
  131. switch val := value.(type) {
  132. case error:
  133. return LogField{Key: key, Value: val.Error()}
  134. case []error:
  135. var errs []string
  136. for _, err := range val {
  137. errs = append(errs, err.Error())
  138. }
  139. return LogField{Key: key, Value: errs}
  140. case time.Duration:
  141. return LogField{Key: key, Value: fmt.Sprint(val)}
  142. case []time.Duration:
  143. var durs []string
  144. for _, dur := range val {
  145. durs = append(durs, fmt.Sprint(dur))
  146. }
  147. return LogField{Key: key, Value: durs}
  148. case []time.Time:
  149. var times []string
  150. for _, t := range val {
  151. times = append(times, fmt.Sprint(t))
  152. }
  153. return LogField{Key: key, Value: times}
  154. case fmt.Stringer:
  155. return LogField{Key: key, Value: val.String()}
  156. case []fmt.Stringer:
  157. var strs []string
  158. for _, str := range val {
  159. strs = append(strs, str.String())
  160. }
  161. return LogField{Key: key, Value: strs}
  162. default:
  163. return LogField{Key: key, Value: val}
  164. }
  165. }
  166. // Info writes v into access log.
  167. func Info(v ...any) {
  168. if shallLog(InfoLevel) {
  169. writeInfo(fmt.Sprint(v...))
  170. }
  171. }
  172. // Infof writes v with format into access log.
  173. func Infof(format string, v ...any) {
  174. if shallLog(InfoLevel) {
  175. writeInfo(fmt.Sprintf(format, v...))
  176. }
  177. }
  178. // Infov writes v into access log with json content.
  179. func Infov(v any) {
  180. if shallLog(InfoLevel) {
  181. writeInfo(v)
  182. }
  183. }
  184. // Infow writes msg along with fields into access log.
  185. func Infow(msg string, fields ...LogField) {
  186. if shallLog(InfoLevel) {
  187. writeInfo(msg, fields...)
  188. }
  189. }
  190. // Must checks if err is nil, otherwise logs the error and exits.
  191. func Must(err error) {
  192. if err == nil {
  193. return
  194. }
  195. msg := fmt.Sprintf("%+v\n\n%s", err.Error(), debug.Stack())
  196. log.Print(msg)
  197. getWriter().Severe(msg)
  198. if ExitOnFatal.True() {
  199. os.Exit(1)
  200. } else {
  201. panic(msg)
  202. }
  203. }
  204. // MustSetup sets up logging with given config c. It exits on error.
  205. func MustSetup(c LogConf) {
  206. Must(SetUp(c))
  207. }
  208. // Reset clears the writer and resets the log level.
  209. func Reset() Writer {
  210. return writer.Swap(nil)
  211. }
  212. // SetLevel sets the logging level. It can be used to suppress some logs.
  213. func SetLevel(level uint32) {
  214. atomic.StoreUint32(&logLevel, level)
  215. }
  216. // SetWriter sets the logging writer. It can be used to customize the logging.
  217. func SetWriter(w Writer) {
  218. if atomic.LoadUint32(&disableLog) == 0 {
  219. writer.Store(w)
  220. }
  221. }
  222. // SetUp sets up the logx. If already set up, just return nil.
  223. // we allow SetUp to be called multiple times, because for example
  224. // we need to allow different service frameworks to initialize logx respectively.
  225. func SetUp(c LogConf) (err error) {
  226. // Just ignore the subsequent SetUp calls.
  227. // Because multiple services in one process might call SetUp respectively.
  228. // Need to wait for the first caller to complete the execution.
  229. setupOnce.Do(func() {
  230. setupLogLevel(c)
  231. if !c.Stat {
  232. DisableStat()
  233. }
  234. if len(c.TimeFormat) > 0 {
  235. timeFormat = c.TimeFormat
  236. }
  237. atomic.StoreUint32(&maxContentLength, c.MaxContentLength)
  238. switch c.Encoding {
  239. case plainEncoding:
  240. atomic.StoreUint32(&encoding, plainEncodingType)
  241. default:
  242. atomic.StoreUint32(&encoding, jsonEncodingType)
  243. }
  244. switch c.Mode {
  245. case fileMode:
  246. err = setupWithFiles(c)
  247. case volumeMode:
  248. err = setupWithVolume(c)
  249. default:
  250. setupWithConsole()
  251. }
  252. })
  253. return
  254. }
  255. // Severe writes v into severe log.
  256. func Severe(v ...any) {
  257. if shallLog(SevereLevel) {
  258. writeSevere(fmt.Sprint(v...))
  259. }
  260. }
  261. // Severef writes v with format into severe log.
  262. func Severef(format string, v ...any) {
  263. if shallLog(SevereLevel) {
  264. writeSevere(fmt.Sprintf(format, v...))
  265. }
  266. }
  267. // Slow writes v into slow log.
  268. func Slow(v ...any) {
  269. if shallLog(ErrorLevel) {
  270. writeSlow(fmt.Sprint(v...))
  271. }
  272. }
  273. // Slowf writes v with format into slow log.
  274. func Slowf(format string, v ...any) {
  275. if shallLog(ErrorLevel) {
  276. writeSlow(fmt.Sprintf(format, v...))
  277. }
  278. }
  279. // Slowv writes v into slow log with json content.
  280. func Slowv(v any) {
  281. if shallLog(ErrorLevel) {
  282. writeSlow(v)
  283. }
  284. }
  285. // Sloww writes msg along with fields into slow log.
  286. func Sloww(msg string, fields ...LogField) {
  287. if shallLog(ErrorLevel) {
  288. writeSlow(msg, fields...)
  289. }
  290. }
  291. // Stat writes v into stat log.
  292. func Stat(v ...any) {
  293. if shallLogStat() && shallLog(InfoLevel) {
  294. writeStat(fmt.Sprint(v...))
  295. }
  296. }
  297. // Statf writes v with format into stat log.
  298. func Statf(format string, v ...any) {
  299. if shallLogStat() && shallLog(InfoLevel) {
  300. writeStat(fmt.Sprintf(format, v...))
  301. }
  302. }
  303. // WithCooldownMillis customizes logging on writing call stack interval.
  304. func WithCooldownMillis(millis int) LogOption {
  305. return func(opts *logOptions) {
  306. opts.logStackCooldownMills = millis
  307. }
  308. }
  309. // WithKeepDays customizes logging to keep logs with days.
  310. func WithKeepDays(days int) LogOption {
  311. return func(opts *logOptions) {
  312. opts.keepDays = days
  313. }
  314. }
  315. // WithGzip customizes logging to automatically gzip the log files.
  316. func WithGzip() LogOption {
  317. return func(opts *logOptions) {
  318. opts.gzipEnabled = true
  319. }
  320. }
  321. // WithMaxBackups customizes how many log files backups will be kept.
  322. func WithMaxBackups(count int) LogOption {
  323. return func(opts *logOptions) {
  324. opts.maxBackups = count
  325. }
  326. }
  327. // WithMaxSize customizes how much space the writing log file can take up.
  328. func WithMaxSize(size int) LogOption {
  329. return func(opts *logOptions) {
  330. opts.maxSize = size
  331. }
  332. }
  333. // WithRotation customizes which log rotation rule to use.
  334. func WithRotation(r string) LogOption {
  335. return func(opts *logOptions) {
  336. opts.rotationRule = r
  337. }
  338. }
  339. func addCaller(fields ...LogField) []LogField {
  340. return append(fields, Field(callerKey, getCaller(callerDepth)))
  341. }
  342. func createOutput(path string) (io.WriteCloser, error) {
  343. if len(path) == 0 {
  344. return nil, ErrLogPathNotSet
  345. }
  346. var rule RotateRule
  347. switch options.rotationRule {
  348. case sizeRotationRule:
  349. rule = NewSizeLimitRotateRule(path, backupFileDelimiter, options.keepDays, options.maxSize,
  350. options.maxBackups, options.gzipEnabled)
  351. default:
  352. rule = DefaultRotateRule(path, backupFileDelimiter, options.keepDays, options.gzipEnabled)
  353. }
  354. return NewLogger(path, rule, options.gzipEnabled)
  355. }
  356. func getWriter() Writer {
  357. w := writer.Load()
  358. if w == nil {
  359. w = writer.StoreIfNil(newConsoleWriter())
  360. }
  361. return w
  362. }
  363. func handleOptions(opts []LogOption) {
  364. for _, opt := range opts {
  365. opt(&options)
  366. }
  367. }
  368. func setupLogLevel(c LogConf) {
  369. switch c.Level {
  370. case levelDebug:
  371. SetLevel(DebugLevel)
  372. case levelInfo:
  373. SetLevel(InfoLevel)
  374. case levelError:
  375. SetLevel(ErrorLevel)
  376. case levelSevere:
  377. SetLevel(SevereLevel)
  378. }
  379. }
  380. func setupWithConsole() {
  381. SetWriter(newConsoleWriter())
  382. }
  383. func setupWithFiles(c LogConf) error {
  384. w, err := newFileWriter(c)
  385. if err != nil {
  386. return err
  387. }
  388. SetWriter(w)
  389. return nil
  390. }
  391. func setupWithVolume(c LogConf) error {
  392. if len(c.ServiceName) == 0 {
  393. return ErrLogServiceNameNotSet
  394. }
  395. c.Path = path.Join(c.Path, c.ServiceName, sysx.Hostname())
  396. return setupWithFiles(c)
  397. }
  398. func shallLog(level uint32) bool {
  399. return atomic.LoadUint32(&logLevel) <= level
  400. }
  401. func shallLogStat() bool {
  402. return atomic.LoadUint32(&disableStat) == 0
  403. }
  404. // writeDebug writes v into debug log.
  405. // Not checking shallLog here is for performance consideration.
  406. // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
  407. // The caller should check shallLog before calling this function.
  408. func writeDebug(val any, fields ...LogField) {
  409. getWriter().Debug(val, addCaller(fields...)...)
  410. }
  411. // writeError writes v into error log.
  412. // Not checking shallLog here is for performance consideration.
  413. // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
  414. // The caller should check shallLog before calling this function.
  415. func writeError(val any, fields ...LogField) {
  416. getWriter().Error(val, addCaller(fields...)...)
  417. }
  418. // writeInfo writes v into info log.
  419. // Not checking shallLog here is for performance consideration.
  420. // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
  421. // The caller should check shallLog before calling this function.
  422. func writeInfo(val any, fields ...LogField) {
  423. getWriter().Info(val, addCaller(fields...)...)
  424. }
  425. // writeSevere writes v into severe log.
  426. // Not checking shallLog here is for performance consideration.
  427. // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
  428. // The caller should check shallLog before calling this function.
  429. func writeSevere(msg string) {
  430. getWriter().Severe(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
  431. }
  432. // writeSlow writes v into slow log.
  433. // Not checking shallLog here is for performance consideration.
  434. // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
  435. // The caller should check shallLog before calling this function.
  436. func writeSlow(val any, fields ...LogField) {
  437. getWriter().Slow(val, addCaller(fields...)...)
  438. }
  439. // writeStack writes v into stack log.
  440. // Not checking shallLog here is for performance consideration.
  441. // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
  442. // The caller should check shallLog before calling this function.
  443. func writeStack(msg string) {
  444. getWriter().Stack(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
  445. }
  446. // writeStat writes v into stat log.
  447. // Not checking shallLog here is for performance consideration.
  448. // If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
  449. // The caller should check shallLog before calling this function.
  450. func writeStat(msg string) {
  451. getWriter().Stat(msg, addCaller()...)
  452. }