default.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package log
  2. import (
  3. "context"
  4. "io"
  5. "os"
  6. "time"
  7. F "github.com/sagernet/sing/common/format"
  8. )
  9. var _ Factory = (*simpleFactory)(nil)
  10. type simpleFactory struct {
  11. formatter Formatter
  12. writer io.Writer
  13. level Level
  14. }
  15. func NewFactory(formatter Formatter, writer io.Writer) Factory {
  16. return &simpleFactory{
  17. formatter: formatter,
  18. writer: writer,
  19. level: LevelTrace,
  20. }
  21. }
  22. func (f *simpleFactory) Level() Level {
  23. return f.level
  24. }
  25. func (f *simpleFactory) SetLevel(level Level) {
  26. f.level = level
  27. }
  28. func (f *simpleFactory) Logger() ContextLogger {
  29. return f.NewLogger("")
  30. }
  31. func (f *simpleFactory) NewLogger(tag string) ContextLogger {
  32. return &simpleLogger{f, tag}
  33. }
  34. var _ ContextLogger = (*simpleLogger)(nil)
  35. type simpleLogger struct {
  36. *simpleFactory
  37. tag string
  38. }
  39. func (l *simpleLogger) Log(ctx context.Context, level Level, args []any) {
  40. level = OverrideLevelFromContext(level, ctx)
  41. if level > l.level {
  42. return
  43. }
  44. message := l.formatter.Format(ctx, level, l.tag, F.ToString(args...), time.Now())
  45. if level == LevelPanic {
  46. panic(message)
  47. }
  48. l.writer.Write([]byte(message))
  49. if level == LevelFatal {
  50. os.Exit(1)
  51. }
  52. }
  53. func (l *simpleLogger) Trace(args ...any) {
  54. l.TraceContext(context.Background(), args...)
  55. }
  56. func (l *simpleLogger) Debug(args ...any) {
  57. l.DebugContext(context.Background(), args...)
  58. }
  59. func (l *simpleLogger) Info(args ...any) {
  60. l.InfoContext(context.Background(), args...)
  61. }
  62. func (l *simpleLogger) Warn(args ...any) {
  63. l.WarnContext(context.Background(), args...)
  64. }
  65. func (l *simpleLogger) Error(args ...any) {
  66. l.ErrorContext(context.Background(), args...)
  67. }
  68. func (l *simpleLogger) Fatal(args ...any) {
  69. l.FatalContext(context.Background(), args...)
  70. }
  71. func (l *simpleLogger) Panic(args ...any) {
  72. l.PanicContext(context.Background(), args...)
  73. }
  74. func (l *simpleLogger) TraceContext(ctx context.Context, args ...any) {
  75. l.Log(ctx, LevelTrace, args)
  76. }
  77. func (l *simpleLogger) DebugContext(ctx context.Context, args ...any) {
  78. l.Log(ctx, LevelDebug, args)
  79. }
  80. func (l *simpleLogger) InfoContext(ctx context.Context, args ...any) {
  81. l.Log(ctx, LevelInfo, args)
  82. }
  83. func (l *simpleLogger) WarnContext(ctx context.Context, args ...any) {
  84. l.Log(ctx, LevelWarn, args)
  85. }
  86. func (l *simpleLogger) ErrorContext(ctx context.Context, args ...any) {
  87. l.Log(ctx, LevelError, args)
  88. }
  89. func (l *simpleLogger) FatalContext(ctx context.Context, args ...any) {
  90. l.Log(ctx, LevelFatal, args)
  91. }
  92. func (l *simpleLogger) PanicContext(ctx context.Context, args ...any) {
  93. l.Log(ctx, LevelPanic, args)
  94. }