logs.go 10 KB

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