Home / Function/ TestLoggerWithConfigFormatting() — gin Function Reference

TestLoggerWithConfigFormatting() — gin Function Reference

Architecture documentation for the TestLoggerWithConfigFormatting() function in logger_test.go from the gin codebase.

Entity Profile

Dependency Diagram

graph TD
  79c5cd0e_ac12_5bd8_dbfb_a67563557c4c["TestLoggerWithConfigFormatting()"]
  9089ba1c_3991_2941_2a87_9934a9e12a6a["logger_test.go"]
  79c5cd0e_ac12_5bd8_dbfb_a67563557c4c -->|defined in| 9089ba1c_3991_2941_2a87_9934a9e12a6a
  style 79c5cd0e_ac12_5bd8_dbfb_a67563557c4c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

logger_test.go lines 182–232

func TestLoggerWithConfigFormatting(t *testing.T) {
	var gotParam LogFormatterParams
	var gotKeys map[any]any
	buffer := new(strings.Builder)

	router := New()
	router.engine.trustedCIDRs, _ = router.engine.prepareTrustedCIDRs()

	router.Use(LoggerWithConfig(LoggerConfig{
		Output: buffer,
		Formatter: func(param LogFormatterParams) string {
			// for assert test
			gotParam = param

			return fmt.Sprintf("[FORMATTER TEST] %v | %3d | %13v | %15s | %-7s %s\n%s",
				param.TimeStamp.Format("2006/01/02 - 15:04:05"),
				param.StatusCode,
				param.Latency,
				param.ClientIP,
				param.Method,
				param.Path,
				param.ErrorMessage,
			)
		},
	}))
	router.GET("/example", func(c *Context) {
		// set dummy ClientIP
		c.Request.Header.Set("X-Forwarded-For", "20.20.20.20")
		gotKeys = c.Keys
		time.Sleep(time.Millisecond)
	})
	PerformRequest(router, http.MethodGet, "/example?a=100")

	// output test
	assert.Contains(t, buffer.String(), "[FORMATTER TEST]")
	assert.Contains(t, buffer.String(), "200")
	assert.Contains(t, buffer.String(), http.MethodGet)
	assert.Contains(t, buffer.String(), "/example")
	assert.Contains(t, buffer.String(), "a=100")

	// LogFormatterParams test
	assert.NotNil(t, gotParam.Request)
	assert.NotEmpty(t, gotParam.TimeStamp)
	assert.Equal(t, 200, gotParam.StatusCode)
	assert.NotEmpty(t, gotParam.Latency)
	assert.Equal(t, "20.20.20.20", gotParam.ClientIP)
	assert.Equal(t, http.MethodGet, gotParam.Method)
	assert.Equal(t, "/example?a=100", gotParam.Path)
	assert.Empty(t, gotParam.ErrorMessage)
	assert.Equal(t, gotKeys, gotParam.Keys)
}

Domain

Subdomains

Defined In

Frequently Asked Questions

What does TestLoggerWithConfigFormatting() do?
TestLoggerWithConfigFormatting() is a function in the gin codebase, defined in logger_test.go.
Where is TestLoggerWithConfigFormatting() defined?
TestLoggerWithConfigFormatting() is defined in logger_test.go at line 182.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free