@@ -3,6 +3,7 @@ package logging
33import (
44 "fmt"
55 "os"
6+ "regexp"
67 "strings"
78 "testing"
89 "time"
@@ -11,30 +12,64 @@ import (
1112func TestStandardLogging (t * testing.T ) {
1213 lname := fmt .Sprintf ("%s/test-standard-log-%d.log" , os .TempDir (), time .Now ().Unix ())
1314
14- logger := NewFileLogger (lname , "" )
15+ logger := NewFileLogger (lname , "" , "" )
1516 defer func () {
1617 os .Remove (lname )
1718 }()
1819
1920 msg := "TestStandardLogging"
20- logger .Printf (msg )
21+ logger .Print (msg )
2122 CloseAllOpenFileLoggers ()
2223
2324 output , err := os .ReadFile (lname )
2425 if err != nil {
2526 t .Errorf ("Unable to read file: %v" , err )
2627 }
2728
28- if ! strings .Contains (string (output ), msg ) {
29- t .Errorf ("Expected '%s', got '%s'" , msg , output )
29+ outstr := string (output )
30+ // Expect format: YYYY-MM-DD HH:MM:SS <msg>
31+ re := regexp .MustCompile (`^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} ` )
32+ if ! re .MatchString (outstr ) {
33+ t .Errorf ("expected timestamp prefix, got: %q" , outstr )
34+ }
35+ if ! strings .Contains (outstr , msg ) {
36+ t .Errorf ("Expected '%s', got '%s'" , msg , outstr )
37+ }
38+ }
39+
40+ func TestStandardTwelveHourLogging (t * testing.T ) {
41+ lname := fmt .Sprintf ("%s/test-standard-log-%d.log" , os .TempDir (), time .Now ().Unix ())
42+
43+ logger := NewFileLogger (lname , "" , "2006/01/02 03:04:05 PM" )
44+ defer func () {
45+ os .Remove (lname )
46+ }()
47+
48+ msg := "TestStandardLogging"
49+ logger .Print (msg )
50+ CloseAllOpenFileLoggers ()
51+
52+ output , err := os .ReadFile (lname )
53+ if err != nil {
54+ t .Errorf ("Unable to read file: %v" , err )
55+ }
56+
57+ outstr := string (output )
58+ // Expect format: YYYY/MM/DD HH:MM:SS [AM|PM] <msg>
59+ re := regexp .MustCompile (`^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} [APM]{2} ` )
60+ if ! re .MatchString (outstr ) {
61+ t .Errorf ("expected timestamp prefix, got: %q" , outstr )
62+ }
63+ if ! strings .Contains (outstr , msg ) {
64+ t .Errorf ("Expected '%s', got '%s'" , msg , outstr )
3065 }
3166}
3267
3368func TestRollingLog (t * testing.T ) {
3469 lname := fmt .Sprintf ("%s/test-rolling-log-%d.log" , os .TempDir (), time .Now ().Unix ())
3570
3671 // Create the logger with log rotation for max 5 backup files.
37- logger := NewFileLoggerWithMaxSize (lname , "" , 1024 , 5 )
72+ logger := NewFileLoggerWithMaxSize (lname , "" , 1024 , 5 , "2006.01.02 03:04:05 PM" )
3873 defer func () {
3974 // Clean up all the log files after the test.
4075 for i := 0 ; i <= 5 ; i ++ { // Remove the main log file and the 5 backups.
@@ -59,8 +94,13 @@ func TestRollingLog(t *testing.T) {
5994 if err != nil {
6095 t .Errorf ("Unable to read file: %v" , err )
6196 }
62- if ! strings .Contains (string (output ), msg ) {
63- t .Errorf ("Expected '%s', got '%s'" , msg , output )
97+ outputStr := string (output )
98+ if ! strings .Contains (outputStr , msg ) {
99+ t .Errorf ("Expected '%s', got '%s'" , msg , outputStr )
100+ }
101+ re := regexp .MustCompile (`^\d{4}.\d{2}.\d{2} \d{2}:\d{2}:\d{2} [APM]{2} ` )
102+ if ! re .MatchString (outputStr ) {
103+ t .Errorf ("expected timestamp prefix, got: %q" , outputStr )
64104 }
65105 rolledFileName = fmt .Sprintf ("%s.%d" , lname , i + 1 ) // lname.1, lname.2, etc.
66106 }
@@ -74,9 +114,8 @@ func TestRollingLog(t *testing.T) {
74114
75115func TestRollingLogFlush_IsFlushed (t * testing.T ) {
76116 // Arrange
77- //lname := fmt.Sprintf("%s/test-flushed-log-%d.log", os.TempDir(), time.Now().Unix())
78117 lname := fmt .Sprintf ("test-flushed-log-%d.log" , time .Now ().Unix ())
79- logger := NewFileLoggerWithMaxSize (lname , "" , 10024 , 5 )
118+ logger := NewFileLoggerWithMaxSize (lname , "" , 10024 , 5 , "" )
80119 defer func () {
81120 CloseAllOpenFileLoggers ()
82121 os .Remove (lname )
0 commit comments