mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
106 lines
3.5 KiB
Go
106 lines
3.5 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package routing
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/hanzoai/git/modules/log"
|
|
"github.com/hanzoai/git/modules/web/types"
|
|
)
|
|
|
|
var (
|
|
startMessage = log.NewColoredValue("started ", log.DEBUG.ColorAttributes()...)
|
|
slowMessage = log.NewColoredValue("slow ", log.WARN.ColorAttributes()...)
|
|
pollingMessage = log.NewColoredValue("polling ", log.INFO.ColorAttributes()...)
|
|
failedMessage = log.NewColoredValue("failed ", log.WARN.ColorAttributes()...)
|
|
completedMessage = log.NewColoredValue("completed", log.INFO.ColorAttributes()...)
|
|
unknownHandlerMessage = log.NewColoredValue("completed", log.ERROR.ColorAttributes()...)
|
|
)
|
|
|
|
func logPrinter(logger log.Logger) func(trigger Event, record *requestRecord) {
|
|
const callerName = "HTTPRequest"
|
|
logRequest := func(level log.Level, fmt string, args ...any) {
|
|
logger.Log(2, &log.Event{Level: level, Caller: callerName}, fmt, args...)
|
|
}
|
|
return func(trigger Event, record *requestRecord) {
|
|
if trigger == StartEvent {
|
|
if !logger.LevelEnabled(log.TRACE) {
|
|
// for performance, if the "started" message shouldn't be logged, we just return as early as possible
|
|
// developers can set the router log level to TRACE to get the "started" request messages.
|
|
return
|
|
}
|
|
// when a request starts, we have no information about the handler function information, we only have the request path
|
|
req := record.request
|
|
logRequest(log.TRACE, "router: %s %v %s for %s", startMessage, log.ColoredMethod(req.Method), req.RequestURI, req.RemoteAddr)
|
|
return
|
|
}
|
|
|
|
req := record.request
|
|
|
|
// Get data from the record
|
|
record.lock.Lock()
|
|
handlerFuncInfo := record.funcInfo.String()
|
|
isLongPolling := record.isLongPolling
|
|
isUnknownHandler := record.funcInfo == nil
|
|
panicErr := record.panicError
|
|
record.lock.Unlock()
|
|
|
|
if trigger == StillExecutingEvent {
|
|
message := slowMessage
|
|
logLevel := log.WARN
|
|
if isLongPolling {
|
|
logLevel = log.INFO
|
|
message = pollingMessage
|
|
}
|
|
logRequest(logLevel, "router: %s %v %s for %s, elapsed %v @ %s",
|
|
message,
|
|
log.ColoredMethod(req.Method), req.RequestURI, req.RemoteAddr,
|
|
log.ColoredTime(time.Since(record.startTime)),
|
|
handlerFuncInfo,
|
|
)
|
|
return
|
|
}
|
|
|
|
if panicErr != nil {
|
|
logRequest(log.WARN, "router: %s %v %s for %s, panic in %v @ %s, err=%v",
|
|
failedMessage,
|
|
log.ColoredMethod(req.Method), req.RequestURI, req.RemoteAddr,
|
|
log.ColoredTime(time.Since(record.startTime)),
|
|
handlerFuncInfo,
|
|
panicErr,
|
|
)
|
|
return
|
|
}
|
|
|
|
var status int
|
|
if v, ok := record.respWriter.(types.ResponseStatusProvider); ok {
|
|
status = v.WrittenStatus()
|
|
}
|
|
logLevel := record.logLevel
|
|
if logLevel == log.UNDEFINED {
|
|
logLevel = log.INFO
|
|
}
|
|
// lower the log level for some specific requests, in most cases these logs are not useful
|
|
if status > 0 && status < 400 &&
|
|
req.RequestURI == "/api/actions/runner.v1.RunnerService/FetchTask" /* Actions Runner polling */ {
|
|
logLevel = log.TRACE
|
|
}
|
|
message := completedMessage
|
|
if isUnknownHandler {
|
|
logLevel = log.ERROR
|
|
message = unknownHandlerMessage
|
|
}
|
|
|
|
logRequest(logLevel, "router: %s %v %s for %s, %v %v in %v @ %s",
|
|
message,
|
|
log.ColoredMethod(req.Method), req.RequestURI, req.RemoteAddr,
|
|
log.ColoredStatus(status), log.ColoredStatus(status, http.StatusText(status)), log.ColoredTime(time.Since(record.startTime)),
|
|
handlerFuncInfo,
|
|
)
|
|
}
|
|
}
|