default.go 3.0 KB

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