Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/gofr/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ func checkIfTerminal(w io.Writer) bool {
}
}

// IsTerminal returns true if the logger's output is a terminal (TTY).
// This helps decide whether to include ANSI color codes for pretty printing.
func (l *logger) IsTerminal() bool {
return l.isTerminal
}

// ChangeLevel changes the log level of the logger.
// This allows dynamic adjustment of the logging verbosity.
func (l *logger) ChangeLevel(level Level) {
Expand Down
39 changes: 31 additions & 8 deletions pkg/gofr/logging/remotelogger/dynamic_level_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ type httpLogFilter struct {
initLogged bool
}

// isTerminalLogger checks if the underlying logger supports terminal output (TTY).
// Returns true if colors and pretty-printing can be applied safely.
func (f *httpLogFilter) isTerminalLogger() bool {
type terminalChecker interface {
IsTerminal() bool
}

if l, ok := f.Logger.(terminalChecker); ok {
return l.IsTerminal()
}

return false
}

// Log implements a simplified filtering strategy with consistent formatting.
func (f *httpLogFilter) Log(args ...any) {
if len(args) == 0 || args[0] == nil {
Expand Down Expand Up @@ -78,14 +92,23 @@ func (f *httpLogFilter) handleHTTPLog(httpLog *service.Log, args []any) {
// Subsequent successful hits - log at DEBUG level with consistent format
case isSuccessful:
if debugLogger, ok := f.Logger.(interface{ Debugf(string, ...any) }); ok {
colorCode := colorForResponseCode(httpLog.ResponseCode)
debugLogger.Debugf("\u001B[38;5;8m%s \u001B[38;5;%dm%-6d\u001B[0m %8d\u001B[38;5;8mµs\u001B[0m %s %s",
httpLog.CorrelationID,
colorCode,
httpLog.ResponseCode,
httpLog.ResponseTime,
httpLog.HTTPMethod,
httpLog.URI)
if f.isTerminalLogger() {
colorCode := colorForResponseCode(httpLog.ResponseCode)
debugLogger.Debugf("\u001B[38;5;8m%s \u001B[38;5;%dm%-6d\u001B[0m %8dμs\u001B[0m %s %s",
httpLog.CorrelationID,
colorCode,
httpLog.ResponseCode,
httpLog.ResponseTime,
httpLog.HTTPMethod,
httpLog.URI)
} else {
debugLogger.Debugf("%s %d %dμs %s %s",
httpLog.CorrelationID,
httpLog.ResponseCode,
httpLog.ResponseTime,
httpLog.HTTPMethod,
httpLog.URI)
}
}

// Error responses - pass through to original logger
Expand Down