@@ -41,8 +41,10 @@ func TestLogRequest(t *testing.T) {
4141 assert .Equal (t , 3 , len (lines ))
4242 assert .Contains (t , lines [0 ], "TRC REQ GET /" )
4343 assert .Contains (t , lines [0 ], id )
44+ assert .Contains (t , lines [0 ], "path=/" )
4445 assert .Contains (t , lines [1 ], "DBG RES GET /" )
4546 assert .Contains (t , lines [1 ], id )
47+ assert .Contains (t , lines [0 ], "path=/" )
4648}
4749
4850func TestLogExtra (t * testing.T ) {
@@ -72,6 +74,7 @@ func TestLogExtra(t *testing.T) {
7274 assert .Equal (t , 2 , len (lines ))
7375 assert .Contains (t , lines [0 ], "WRN TEST" )
7476 assert .Contains (t , lines [0 ], id )
77+ assert .Contains (t , lines [0 ], "path=/" )
7578}
7679
7780func TestLogChangeRequestLevel (t * testing.T ) {
@@ -104,3 +107,41 @@ func TestLogChangeRequestLevel(t *testing.T) {
104107 assert .Contains (t , lines [1 ], "DBG RES GET /" )
105108 assert .Contains (t , lines [1 ], id )
106109}
110+
111+ func TestLogAgent (t * testing.T ) {
112+ gin .SetMode (gin .ReleaseMode )
113+ zerolog .SetGlobalLevel (zerolog .TraceLevel )
114+
115+ buf := & bytes.Buffer {}
116+ log .Logger = zerolog .New (buf ).With ().Timestamp ().Logger ().
117+ Output (zerolog.ConsoleWriter {Out : buf , TimeFormat : time .RFC3339 , NoColor : true })
118+
119+ w := httptest .NewRecorder ()
120+ e := gin .New ()
121+
122+ e .GET ("" , Logger (zerolog .TraceLevel ), func (ctx * gin.Context ) {
123+ GetLogger (ctx ).Warn ().Msg ("TEST" )
124+ })
125+
126+ req , _ := http .NewRequest ("GET" , "/" , nil )
127+ req .Header .Set ("Origin" , "http://localhost:8080" )
128+ req .Header .Set ("User-Agent" , "test-agent" )
129+ e .ServeHTTP (w , req )
130+
131+ fmt .Println (buf )
132+ lines := strings .Split (buf .String (), "\n " )
133+ id := w .Header ().Get ("X-Request-ID" )
134+
135+ assert .NotEmpty (t , id )
136+
137+ assert .Equal (t , 4 , len (lines ))
138+ assert .Contains (t , lines [0 ], "TRC REQ GET /" )
139+ assert .Contains (t , lines [0 ], id )
140+ assert .Contains (t , lines [0 ], "agent=test-agent" )
141+ assert .Contains (t , lines [1 ], "WRN TEST" )
142+ assert .Contains (t , lines [1 ], id )
143+ assert .Contains (t , lines [1 ], "agent=test-agent" )
144+ assert .Contains (t , lines [2 ], "DBG RES GET /" )
145+ assert .Contains (t , lines [2 ], id )
146+ assert .Contains (t , lines [2 ], "agent=test-agent" )
147+ }
0 commit comments