@@ -3,6 +3,7 @@ package logger
33import (
44 "bytes"
55 "os"
6+ "path/filepath"
67 "strings"
78 "testing"
89 "time"
@@ -391,3 +392,117 @@ func TestComputeEnabled(t *testing.T) {
391392 })
392393 }
393394}
395+
396+ func TestDebugLoggerWritesToFile (t * testing.T ) {
397+ // Create a temporary directory for the file logger
398+ tmpDir := t .TempDir ()
399+ logDir := filepath .Join (tmpDir , "logs" )
400+ fileName := "debug-test.log"
401+
402+ // Initialize the file logger
403+ err := InitFileLogger (logDir , fileName )
404+ if err != nil {
405+ t .Fatalf ("InitFileLogger failed: %v" , err )
406+ }
407+ defer CloseGlobalLogger ()
408+
409+ // Enable all debug loggers
410+ debugEnv = "*"
411+
412+ // Create a debug logger
413+ log := New ("test:debug" )
414+
415+ // Capture stderr to verify stderr output
416+ stderrOutput := captureStderr (func () {
417+ log .Printf ("Test message %d" , 42 )
418+ log .Print ("Another test message" )
419+ })
420+
421+ // Verify stderr output contains the messages
422+ if ! strings .Contains (stderrOutput , "Test message 42" ) {
423+ t .Errorf ("Stderr should contain debug message, got: %s" , stderrOutput )
424+ }
425+ if ! strings .Contains (stderrOutput , "Another test message" ) {
426+ t .Errorf ("Stderr should contain debug message, got: %s" , stderrOutput )
427+ }
428+
429+ // Close the file logger to flush all data
430+ CloseGlobalLogger ()
431+
432+ // Read the log file
433+ logPath := filepath .Join (logDir , fileName )
434+ content , err := os .ReadFile (logPath )
435+ if err != nil {
436+ t .Fatalf ("Failed to read log file: %v" , err )
437+ }
438+
439+ logContent := string (content )
440+
441+ // Verify the file logger contains the same messages (text-only, no colors)
442+ if ! strings .Contains (logContent , "Test message 42" ) {
443+ t .Errorf ("Log file should contain debug message, got: %s" , logContent )
444+ }
445+ if ! strings .Contains (logContent , "Another test message" ) {
446+ t .Errorf ("Log file should contain debug message, got: %s" , logContent )
447+ }
448+
449+ // Verify the file logger has DEBUG level
450+ if ! strings .Contains (logContent , "[DEBUG]" ) {
451+ t .Errorf ("Log file should contain [DEBUG] level, got: %s" , logContent )
452+ }
453+
454+ // Verify the file logger has the namespace as category
455+ if ! strings .Contains (logContent , "[test:debug]" ) {
456+ t .Errorf ("Log file should contain [test:debug] category, got: %s" , logContent )
457+ }
458+
459+ // Verify no color codes in file output
460+ if strings .Contains (logContent , "\033 [" ) {
461+ t .Errorf ("Log file should not contain ANSI color codes, got: %s" , logContent )
462+ }
463+ }
464+
465+ func TestDebugLoggerDisabledNoFileWrite (t * testing.T ) {
466+ // Create a temporary directory for the file logger
467+ tmpDir := t .TempDir ()
468+ logDir := filepath .Join (tmpDir , "logs" )
469+ fileName := "debug-disabled-test.log"
470+
471+ // Initialize the file logger
472+ err := InitFileLogger (logDir , fileName )
473+ if err != nil {
474+ t .Fatalf ("InitFileLogger failed: %v" , err )
475+ }
476+ defer CloseGlobalLogger ()
477+
478+ // Disable all debug loggers
479+ debugEnv = ""
480+
481+ // Create a debug logger (should be disabled)
482+ log := New ("test:disabled" )
483+
484+ // Verify logger is disabled
485+ if log .Enabled () {
486+ t .Fatal ("Logger should be disabled when DEBUG is empty" )
487+ }
488+
489+ // Try to log (should not write anywhere)
490+ log .Printf ("This should not appear" )
491+
492+ // Close the file logger to flush all data
493+ CloseGlobalLogger ()
494+
495+ // Read the log file
496+ logPath := filepath .Join (logDir , fileName )
497+ content , err := os .ReadFile (logPath )
498+ if err != nil {
499+ t .Fatalf ("Failed to read log file: %v" , err )
500+ }
501+
502+ logContent := string (content )
503+
504+ // Verify the message is NOT in the file (logger was disabled)
505+ if strings .Contains (logContent , "This should not appear" ) {
506+ t .Errorf ("Disabled logger should not write to file, got: %s" , logContent )
507+ }
508+ }
0 commit comments