options_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package logger
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "go.uber.org/zap"
  7. "go.uber.org/zap/zapcore"
  8. )
  9. func TestResolveLogFilePath_Default(t *testing.T) {
  10. t.Setenv("DATA_DIR", "")
  11. got := resolveLogFilePath("")
  12. if got != DefaultContainerLogPath {
  13. t.Fatalf("resolveLogFilePath() = %q, want %q", got, DefaultContainerLogPath)
  14. }
  15. }
  16. func TestResolveLogFilePath_WithDataDir(t *testing.T) {
  17. t.Setenv("DATA_DIR", "/tmp/sub2api-data")
  18. got := resolveLogFilePath("")
  19. want := filepath.Join("/tmp/sub2api-data", "logs", "sub2api.log")
  20. if got != want {
  21. t.Fatalf("resolveLogFilePath() = %q, want %q", got, want)
  22. }
  23. }
  24. func TestResolveLogFilePath_ExplicitPath(t *testing.T) {
  25. t.Setenv("DATA_DIR", "/tmp/ignore")
  26. got := resolveLogFilePath("/var/log/custom.log")
  27. if got != "/var/log/custom.log" {
  28. t.Fatalf("resolveLogFilePath() = %q, want explicit path", got)
  29. }
  30. }
  31. func TestNormalizedOptions_InvalidFallback(t *testing.T) {
  32. t.Setenv("DATA_DIR", "")
  33. opts := InitOptions{
  34. Level: "TRACE",
  35. Format: "TEXT",
  36. ServiceName: "",
  37. Environment: "",
  38. StacktraceLevel: "panic",
  39. Output: OutputOptions{
  40. ToStdout: false,
  41. ToFile: false,
  42. },
  43. Rotation: RotationOptions{
  44. MaxSizeMB: 0,
  45. MaxBackups: -1,
  46. MaxAgeDays: -1,
  47. },
  48. Sampling: SamplingOptions{
  49. Enabled: true,
  50. Initial: 0,
  51. Thereafter: 0,
  52. },
  53. }
  54. out := opts.normalized()
  55. if out.Level != "trace" {
  56. // normalized 仅做 trim/lower,不做校验;校验在 config 层。
  57. t.Fatalf("normalized level should preserve value for upstream validation, got %q", out.Level)
  58. }
  59. if !out.Output.ToStdout {
  60. t.Fatalf("normalized output should fallback to stdout")
  61. }
  62. if out.Output.FilePath != DefaultContainerLogPath {
  63. t.Fatalf("normalized file path = %q", out.Output.FilePath)
  64. }
  65. if out.Rotation.MaxSizeMB != 100 {
  66. t.Fatalf("normalized max_size_mb = %d", out.Rotation.MaxSizeMB)
  67. }
  68. if out.Rotation.MaxBackups != 10 {
  69. t.Fatalf("normalized max_backups = %d", out.Rotation.MaxBackups)
  70. }
  71. if out.Rotation.MaxAgeDays != 7 {
  72. t.Fatalf("normalized max_age_days = %d", out.Rotation.MaxAgeDays)
  73. }
  74. if out.Sampling.Initial != 100 || out.Sampling.Thereafter != 100 {
  75. t.Fatalf("normalized sampling defaults invalid: %+v", out.Sampling)
  76. }
  77. }
  78. func TestBuildFileCore_InvalidPathFallback(t *testing.T) {
  79. t.Setenv("DATA_DIR", "")
  80. opts := bootstrapOptions()
  81. opts.Output.ToFile = true
  82. opts.Output.FilePath = filepath.Join(os.DevNull, "logs", "sub2api.log")
  83. encoderCfg := zapcore.EncoderConfig{
  84. TimeKey: "time",
  85. LevelKey: "level",
  86. MessageKey: "msg",
  87. EncodeTime: zapcore.ISO8601TimeEncoder,
  88. EncodeLevel: zapcore.CapitalLevelEncoder,
  89. }
  90. encoder := zapcore.NewJSONEncoder(encoderCfg)
  91. _, _, err := buildFileCore(encoder, zap.NewAtomicLevel(), opts)
  92. if err == nil {
  93. t.Fatalf("buildFileCore() expected error for invalid path")
  94. }
  95. }