global.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2019 PingCAP, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package log
  14. import (
  15. "go.uber.org/zap"
  16. "go.uber.org/zap/zapcore"
  17. )
  18. // ZapEncodingName is the encoder name registered in zap
  19. var ZapEncodingName = "pingcap-log"
  20. // Debug logs a message at DebugLevel. The message includes any fields passed
  21. // at the log site, as well as any fields accumulated on the logger.
  22. func Debug(msg string, fields ...zap.Field) {
  23. L().WithOptions(zap.AddCallerSkip(1)).Debug(msg, fields...)
  24. }
  25. // Info logs a message at InfoLevel. The message includes any fields passed
  26. // at the log site, as well as any fields accumulated on the logger.
  27. func Info(msg string, fields ...zap.Field) {
  28. L().WithOptions(zap.AddCallerSkip(1)).Info(msg, fields...)
  29. }
  30. // Warn logs a message at WarnLevel. The message includes any fields passed
  31. // at the log site, as well as any fields accumulated on the logger.
  32. func Warn(msg string, fields ...zap.Field) {
  33. L().WithOptions(zap.AddCallerSkip(1)).Warn(msg, fields...)
  34. }
  35. // Error logs a message at ErrorLevel. The message includes any fields passed
  36. // at the log site, as well as any fields accumulated on the logger.
  37. func Error(msg string, fields ...zap.Field) {
  38. L().WithOptions(zap.AddCallerSkip(1)).Error(msg, fields...)
  39. }
  40. // Panic logs a message at PanicLevel. The message includes any fields passed
  41. // at the log site, as well as any fields accumulated on the logger.
  42. //
  43. // The logger then panics, even if logging at PanicLevel is disabled.
  44. func Panic(msg string, fields ...zap.Field) {
  45. L().WithOptions(zap.AddCallerSkip(1)).Panic(msg, fields...)
  46. }
  47. // Fatal logs a message at FatalLevel. The message includes any fields passed
  48. // at the log site, as well as any fields accumulated on the logger.
  49. //
  50. // The logger then calls os.Exit(1), even if logging at FatalLevel is
  51. // disabled.
  52. func Fatal(msg string, fields ...zap.Field) {
  53. L().WithOptions(zap.AddCallerSkip(1)).Fatal(msg, fields...)
  54. }
  55. // With creates a child logger and adds structured context to it.
  56. // Fields added to the child don't affect the parent, and vice versa.
  57. func With(fields ...zap.Field) *zap.Logger {
  58. return L().WithOptions(zap.AddCallerSkip(1)).With(fields...)
  59. }
  60. // SetLevel alters the logging level.
  61. func SetLevel(l zapcore.Level) {
  62. _globalP.Load().(*ZapProperties).Level.SetLevel(l)
  63. }
  64. // GetLevel gets the logging level.
  65. func GetLevel() zapcore.Level {
  66. return _globalP.Load().(*ZapProperties).Level.Level()
  67. }