logs.go 10 KB

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