Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Macaron to be set to log through to gitea.log #5667

Merged
Merged
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
1 change: 1 addition & 0 deletions custom/conf/app.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ MODE = console
BUFFER_LEN = 10000
; Either "Trace", "Debug", "Info", "Warn", "Error", "Critical", default is "Trace"
LEVEL = Trace
REDIRECT_MACARON_LOG = false
zeripath marked this conversation as resolved.
Show resolved Hide resolved

; For "console" mode only
[log.console]
Expand Down
1 change: 1 addition & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
- `ROOT_PATH`: **\<empty\>**: Root path for log files.
- `MODE`: **console**: Logging mode. For multiple modes, use a comma to separate values.
- `LEVEL`: **Trace**: General log level. \[Trace, Debug, Info, Warn, Error, Critical\]
- `REDIRECT_MACARON_LOG`: **false**: Redirects the Macaron log to the Gitea logger.

## Cron (`cron`)

Expand Down
42 changes: 42 additions & 0 deletions modules/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,48 @@ type LoggerInterface interface {

type loggerType func() LoggerInterface

// LoggerAsWriter is a io.Writer shim around the gitea log
type LoggerAsWriter struct {
level int
}

// NewLoggerAsWriter creates a Writer representation of the logger with setable log level
func NewLoggerAsWriter(level string) *LoggerAsWriter {
l := &LoggerAsWriter{}
switch strings.ToUpper(level) {
case "TRACE":
l.level = TRACE
case "DEBUG":
l.level = DEBUG
case "INFO":
l.level = INFO
case "WARN":
l.level = WARN
case "ERROR":
l.level = ERROR
case "CRITICAL":
l.level = CRITICAL
case "FATAL":
l.level = FATAL
default:
l.level = INFO
}
return l
}

// Write implements the io.Writer interface to allow spoofing of macaron
func (l *LoggerAsWriter) Write(p []byte) (int, error) {
l.Log(string(p))
return len(p), nil
}

// Log takes a given string and logs it at the set log-level
func (l *LoggerAsWriter) Log(msg string) {
for _, logger := range loggers {
logger.writerMsg(0, l.level, msg)
}
}

var adapters = make(map[string]loggerType)

// Register registers given logger provider to adapters.
Expand Down
10 changes: 6 additions & 4 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,11 @@ var (
LibravatarService *libravatar.Libravatar

// Log settings
LogLevel string
LogRootPath string
LogModes []string
LogConfigs []string
LogLevel string
LogRootPath string
LogModes []string
LogConfigs []string
RedirectMacaronLog bool

// Attachment settings
AttachmentPath string
Expand Down Expand Up @@ -767,6 +768,7 @@ func NewContext() {
LogLevel = getLogLevel("log", "LEVEL", "Info")
LogRootPath = Cfg.Section("log").Key("ROOT_PATH").MustString(path.Join(AppWorkPath, "log"))
forcePathSeparator(LogRootPath)
RedirectMacaronLog = Cfg.Section("log").Key("REDIRECT_MACARON_LOG").MustBool(false)

sec := Cfg.Section("server")
AppName = Cfg.Section("").Key("APP_NAME").MustString("Gitea: Git with a cup of tea")
Expand Down
29 changes: 26 additions & 3 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package routes

import (
"encoding/gob"
"fmt"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -45,12 +46,34 @@ import (
macaron "gopkg.in/macaron.v1"
)

func giteaLogger(l *log.LoggerAsWriter) macaron.Handler {
return func(ctx *macaron.Context) {
start := time.Now()

l.Log(fmt.Sprintf("[Macaron] Started %s %s for %s", ctx.Req.Method, ctx.Req.RequestURI, ctx.RemoteAddr()))

ctx.Next()

rw := ctx.Resp.(macaron.ResponseWriter)
l.Log(fmt.Sprintf("[Macaron] Completed %s %s %v %s in %v", ctx.Req.Method, ctx.Req.RequestURI, rw.Status(), http.StatusText(rw.Status()), time.Since(start)))
}
}

// NewMacaron initializes Macaron instance.
func NewMacaron() *macaron.Macaron {
gob.Register(&u2f.Challenge{})
m := macaron.New()
if !setting.DisableRouterLog {
m.Use(macaron.Logger())
var m *macaron.Macaron
if setting.RedirectMacaronLog {
loggerAsWriter := log.NewLoggerAsWriter("INFO")
zeripath marked this conversation as resolved.
Show resolved Hide resolved
m = macaron.NewWithLogger(loggerAsWriter)
if !setting.DisableRouterLog {
m.Use(giteaLogger(loggerAsWriter))
}
} else {
m = macaron.New()
if !setting.DisableRouterLog {
m.Use(macaron.Logger())
}
}
m.Use(macaron.Recovery())
if setting.EnableGzip {
Expand Down