@@ -31,6 +31,52 @@ func TestAuditLoggerDoesNothingWhenDisabled(t *testing.T) {
3131 assert .True (t , len (output ) == 0 , "Expected no output from the audit logger when disabled." )
3232}
3333
34+ func TestAuditLoggerLogln (t * testing.T ) {
35+ InitAuditLogger (false )
36+
37+ defer func (level log.Level ) { defaultLogLevel = level }(defaultLogLevel )
38+ // Set the default log level to panic to make sure that audit log statements are written even if the log level is
39+ // lower than info.
40+ defaultLogLevel = log .PanicLevel
41+
42+ output := captureOutput (t , func () { Audit ().Logln (context .Background (), AuditGRPCAccess , LogFields {}, "Test message" ) })
43+ assert .True (t , len (output ) > 0 , "Expected output from the audit logger when enabled." )
44+ assert .Contains (t , output , auditKey , fmt .Sprintf ("Expected the auditKey: %s to be present in the output." , auditKey ))
45+ assert .Contains (t , output , "Test message" , "Expected message to be present in the output." )
46+ }
47+
48+ func TestAuditLoggerLoglnWhenDisabled (t * testing.T ) {
49+ InitAuditLogger (true )
50+
51+ output := captureOutput (t , func () { Audit ().Logln (context .Background (), AuditGRPCAccess , LogFields {}, "Test message" ) })
52+ assert .True (t , len (output ) == 0 , "Expected no output from the audit logger when disabled." )
53+ }
54+
55+ func TestAuditLoggerLogf (t * testing.T ) {
56+ InitAuditLogger (false )
57+
58+ defer func (level log.Level ) { defaultLogLevel = level }(defaultLogLevel )
59+ // Set the default log level to panic to make sure that audit log statements are written even if the log level is
60+ // lower than info.
61+ defaultLogLevel = log .PanicLevel
62+
63+ output := captureOutput (t , func () {
64+ Audit ().Logf (context .Background (), AuditGRPCAccess , LogFields {}, "Test message with %s" , "formatting" )
65+ })
66+ assert .True (t , len (output ) > 0 , "Expected output from the audit logger when enabled." )
67+ assert .Contains (t , output , auditKey , fmt .Sprintf ("Expected the auditKey: %s to be present in the output." , auditKey ))
68+ assert .Contains (t , output , "Test message with formatting" , "Expected formatted message to be present in the output." )
69+ }
70+
71+ func TestAuditLoggerLogfWhenDisabled (t * testing.T ) {
72+ InitAuditLogger (true )
73+
74+ output := captureOutput (t , func () {
75+ Audit ().Logf (context .Background (), AuditGRPCAccess , LogFields {}, "Test message with %s" , "formatting" )
76+ })
77+ assert .True (t , len (output ) == 0 , "Expected no output from the audit logger when disabled." )
78+ }
79+
3480func captureOutput (t * testing.T , f func ()) string {
3581 var buf bytes.Buffer
3682 log .SetOutput (& buf )
0 commit comments