logs.go 9.9 KB

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