logs.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. }
  37. // LogField is a key-value pair that will be added to the log entry.
  38. LogField struct {
  39. Key string
  40. Value interface{}
  41. }
  42. // LogOption defines the method to customize the logging.
  43. LogOption func(options *logOptions)
  44. )
  45. // Alert alerts v in alert level, and the message is written to error log.
  46. func Alert(v string) {
  47. getWriter().Alert(v)
  48. }
  49. // Close closes the logging.
  50. func Close() error {
  51. if w := writer.Swap(nil); w != nil {
  52. return w.(io.Closer).Close()
  53. }
  54. return nil
  55. }
  56. // Disable disables the logging.
  57. func Disable() {
  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. errorTextSync(fmt.Sprint(v...))
  67. }
  68. // Errorf writes v with format into error log.
  69. func Errorf(format string, v ...interface{}) {
  70. errorTextSync(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. stackSync(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. stackSync(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. errorAnySync(v)
  86. }
  87. // Errorw writes msg along with fields into error log.
  88. func Errorw(msg string, fields ...LogField) {
  89. errorFieldsSync(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 time.Duration:
  95. return LogField{Key: key, Value: fmt.Sprint(val)}
  96. default:
  97. return LogField{Key: key, Value: val}
  98. }
  99. }
  100. // Info writes v into access log.
  101. func Info(v ...interface{}) {
  102. infoTextSync(fmt.Sprint(v...))
  103. }
  104. // Infof writes v with format into access log.
  105. func Infof(format string, v ...interface{}) {
  106. infoTextSync(fmt.Sprintf(format, v...))
  107. }
  108. // Infov writes v into access log with json content.
  109. func Infov(v interface{}) {
  110. infoAnySync(v)
  111. }
  112. // Infow writes msg along with fields into access log.
  113. func Infow(msg string, fields ...LogField) {
  114. infoFieldsSync(msg, fields...)
  115. }
  116. // Must checks if err is nil, otherwise logs the error and exits.
  117. func Must(err error) {
  118. if err == nil {
  119. return
  120. }
  121. msg := err.Error()
  122. log.Print(msg)
  123. getWriter().Severe(msg)
  124. os.Exit(1)
  125. }
  126. // MustSetup sets up logging with given config c. It exits on error.
  127. func MustSetup(c LogConf) {
  128. Must(SetUp(c))
  129. }
  130. // Reset clears the writer and resets the log level.
  131. func Reset() Writer {
  132. SetLevel(InfoLevel)
  133. return writer.Swap(nil)
  134. }
  135. // SetLevel sets the logging level. It can be used to suppress some logs.
  136. func SetLevel(level uint32) {
  137. atomic.StoreUint32(&logLevel, level)
  138. }
  139. // SetWriter sets the logging writer. It can be used to customize the logging.
  140. // Call Reset before calling SetWriter again.
  141. func SetWriter(w Writer) {
  142. if writer.Load() == nil {
  143. writer.Store(w)
  144. }
  145. }
  146. // SetUp sets up the logx. If already set up, just return nil.
  147. // we allow SetUp to be called multiple times, because for example
  148. // we need to allow different service frameworks to initialize logx respectively.
  149. // the same logic for SetUp
  150. func SetUp(c LogConf) error {
  151. setupLogLevel(c)
  152. if len(c.TimeFormat) > 0 {
  153. timeFormat = c.TimeFormat
  154. }
  155. switch c.Encoding {
  156. case plainEncoding:
  157. atomic.StoreUint32(&encoding, plainEncodingType)
  158. default:
  159. atomic.StoreUint32(&encoding, jsonEncodingType)
  160. }
  161. switch c.Mode {
  162. case consoleMode:
  163. setupWithConsole()
  164. return nil
  165. case volumeMode:
  166. return setupWithVolume(c)
  167. default:
  168. return setupWithFiles(c)
  169. }
  170. }
  171. // Severe writes v into severe log.
  172. func Severe(v ...interface{}) {
  173. severeSync(fmt.Sprint(v...))
  174. }
  175. // Severef writes v with format into severe log.
  176. func Severef(format string, v ...interface{}) {
  177. severeSync(fmt.Sprintf(format, v...))
  178. }
  179. // Slow writes v into slow log.
  180. func Slow(v ...interface{}) {
  181. slowTextSync(fmt.Sprint(v...))
  182. }
  183. // Slowf writes v with format into slow log.
  184. func Slowf(format string, v ...interface{}) {
  185. slowTextSync(fmt.Sprintf(format, v...))
  186. }
  187. // Slowv writes v into slow log with json content.
  188. func Slowv(v interface{}) {
  189. slowAnySync(v)
  190. }
  191. // Sloww writes msg along with fields into slow log.
  192. func Sloww(msg string, fields ...LogField) {
  193. slowFieldsSync(msg, fields...)
  194. }
  195. // Stat writes v into stat log.
  196. func Stat(v ...interface{}) {
  197. statSync(fmt.Sprint(v...))
  198. }
  199. // Statf writes v with format into stat log.
  200. func Statf(format string, v ...interface{}) {
  201. statSync(fmt.Sprintf(format, v...))
  202. }
  203. // WithCooldownMillis customizes logging on writing call stack interval.
  204. func WithCooldownMillis(millis int) LogOption {
  205. return func(opts *logOptions) {
  206. opts.logStackCooldownMills = millis
  207. }
  208. }
  209. // WithKeepDays customizes logging to keep logs with days.
  210. func WithKeepDays(days int) LogOption {
  211. return func(opts *logOptions) {
  212. opts.keepDays = days
  213. }
  214. }
  215. // WithGzip customizes logging to automatically gzip the log files.
  216. func WithGzip() LogOption {
  217. return func(opts *logOptions) {
  218. opts.gzipEnabled = true
  219. }
  220. }
  221. func createOutput(path string) (io.WriteCloser, error) {
  222. if len(path) == 0 {
  223. return nil, ErrLogPathNotSet
  224. }
  225. return NewLogger(path, DefaultRotateRule(path, backupFileDelimiter, options.keepDays,
  226. options.gzipEnabled), options.gzipEnabled)
  227. }
  228. func errorAnySync(v interface{}) {
  229. if shallLog(ErrorLevel) {
  230. getWriter().Error(v)
  231. }
  232. }
  233. func errorFieldsSync(content string, fields ...LogField) {
  234. if shallLog(ErrorLevel) {
  235. getWriter().Error(content, fields...)
  236. }
  237. }
  238. func errorTextSync(msg string) {
  239. if shallLog(ErrorLevel) {
  240. getWriter().Error(msg)
  241. }
  242. }
  243. func getWriter() Writer {
  244. w := writer.Load()
  245. if w == nil {
  246. w = newConsoleWriter()
  247. writer.Store(w)
  248. }
  249. return w
  250. }
  251. func handleOptions(opts []LogOption) {
  252. for _, opt := range opts {
  253. opt(&options)
  254. }
  255. }
  256. func infoAnySync(val interface{}) {
  257. if shallLog(InfoLevel) {
  258. getWriter().Info(val)
  259. }
  260. }
  261. func infoFieldsSync(content string, fields ...LogField) {
  262. if shallLog(InfoLevel) {
  263. getWriter().Info(content, fields...)
  264. }
  265. }
  266. func infoTextSync(msg string) {
  267. if shallLog(InfoLevel) {
  268. getWriter().Info(msg)
  269. }
  270. }
  271. func setupLogLevel(c LogConf) {
  272. switch c.Level {
  273. case levelInfo:
  274. SetLevel(InfoLevel)
  275. case levelError:
  276. SetLevel(ErrorLevel)
  277. case levelSevere:
  278. SetLevel(SevereLevel)
  279. }
  280. }
  281. func setupWithConsole() {
  282. SetWriter(newConsoleWriter())
  283. }
  284. func setupWithFiles(c LogConf) error {
  285. w, err := newFileWriter(c)
  286. if err != nil {
  287. return err
  288. }
  289. SetWriter(w)
  290. return nil
  291. }
  292. func setupWithVolume(c LogConf) error {
  293. if len(c.ServiceName) == 0 {
  294. return ErrLogServiceNameNotSet
  295. }
  296. c.Path = path.Join(c.Path, c.ServiceName, sysx.Hostname())
  297. return setupWithFiles(c)
  298. }
  299. func severeSync(msg string) {
  300. if shallLog(SevereLevel) {
  301. getWriter().Severe(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
  302. }
  303. }
  304. func shallLog(level uint32) bool {
  305. return atomic.LoadUint32(&logLevel) <= level
  306. }
  307. func shallLogStat() bool {
  308. return atomic.LoadUint32(&disableStat) == 0
  309. }
  310. func slowAnySync(v interface{}) {
  311. if shallLog(ErrorLevel) {
  312. getWriter().Slow(v)
  313. }
  314. }
  315. func slowFieldsSync(content string, fields ...LogField) {
  316. if shallLog(ErrorLevel) {
  317. getWriter().Slow(content, fields...)
  318. }
  319. }
  320. func slowTextSync(msg string) {
  321. if shallLog(ErrorLevel) {
  322. getWriter().Slow(msg)
  323. }
  324. }
  325. func stackSync(msg string) {
  326. if shallLog(ErrorLevel) {
  327. getWriter().Stack(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
  328. }
  329. }
  330. func statSync(msg string) {
  331. if shallLogStat() && shallLog(InfoLevel) {
  332. getWriter().Stat(msg)
  333. }
  334. }