default.go 2.5 KB

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