Skip to content

Logger update #1213

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

Merged
merged 4 commits into from
Aug 26, 2017
Merged

Logger update #1213

merged 4 commits into from
Aug 26, 2017

Conversation

notzippy
Copy link
Collaborator

@notzippy notzippy commented Jul 26, 2017

This logger provides context logging for the controller, and it provides the ability to output in either json or text file format. Log messages and key value pairs. File rotation is also handled based on either the file size or the number of days specified in the app.config

Logging

By default a new logger is available for the application called revel.AppLog - its interface is using the log15 interface (see here) You can assign log handlers which can perform multiple functions.

NOTE The new logger expects message values in key value pairs. For example revel.AppLog.Debug("Hi there") is fine revel.AppLog.Debug("Hi there", 25) will panic (only passed one argument for the context). revel.AppLog.Debug("Hi there","age",25,"sex","yes","state",254) is fine. and will produce a log message that includes the context age,sex,state.

The logger in the controller adds the ability to extend its context so you can say c.Log = c.Log.New("user",userid) and all subsequent logs using that controller will report the 'user'=userid in the log files. Each request instance in the controller will create a new log context so all your logs messages during the request will be unique. Revel create the c.Log using a context that includes the ip, request path and request method

Default operation

By default it operates the same as the old logging utility. Unless the log file ends in json , in that case the log output will be in json format. Log file size is limited to 10G in size but may be specified in app.conf log.maxsize (in MB). Log file age is limited to 14 days be default unless specified in app.conf log.maxage (in days). If either is exceeded the file is date stamped and a new file is created.

Deprecated loggers

revel.ERROR,revel.WARN,revel.INFO,revel.TRACE, are deprecated. Controllers in applications should log to the context logger controller.Log and jobs should log to revel.AppLog

Sample Screen Shot
image

TODO

  • Go through revel remove all references to deprecated loggers
  • docs for new app.conf

logger.go Outdated
Level log15.Lvl
}

func (s loggerRewrite) Write(p []byte) (n int, err error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need explicit deprecation message to be painfully clear to revel users when running code that uses this deprecated code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s vs lr?

logger.go Outdated
// INFO 2017/07/25 14:53:40 db.go:768: [gorp] begin; [] (138.433µs)
func terminalFormat(noColor bool) log15.Format {
return log15.FormatFunc(func(r *log15.Record) []byte {
var color = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comments for what colors these correspond to

logger.go Outdated
k, v = errorKey, formatLogfmtValue(k)
}

// XXX: we should probably check that all of your key bytes aren't invalid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XXX? Maybe TODO

logger.go Outdated
b := &bytes.Buffer{}
lvl := strings.ToUpper(r.Lvl.String())
if noColor == false && color > 0 {
// fn := findInContext("fn",r.Ctx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete?

logger.go Outdated
fmt.Fprintf(b, "[%s] [%s] %s ", lvl, r.Time.Format(termTimeFormat), r.Msg)
}

// try to justify the log output for short messages
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try

server-engine.go Outdated
@@ -154,13 +156,10 @@ func handleInternal(ctx ServerContext) {
// RequestStartTime ClientIP ResponseStatus RequestLatency HTTPMethod URLPath
// Sample format:
// 2016/05/25 17:46:37.112 127.0.0.1 200 270.157µs GET /
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the comment

@notzippy notzippy added status-doing Implementation has started and removed waffle: needs review labels Aug 2, 2017
@notzippy notzippy force-pushed the logger branch 3 times, most recently from 7f7fb80 to b7ef138 Compare August 4, 2017 02:59
@notzippy notzippy added waffle: needs review and removed status-doing Implementation has started labels Aug 4, 2017
On controller creation a logger is attached to the controller, this logger has contextual information like the clientIp, request path, etc etc. Every log message sent to this logger will add this to the log message
Added ability to log to json files. If log file ends in json then the logger will write to that file
Added ability to define logger.maxsize (in mega bytes) defaults to 10G
Added ability to define logger.maxage (in days) defaults to 7
If either of those values are exceeded the log file is rotated - ie the old is renamed and a new one is created
@notzippy
Copy link
Collaborator Author

notzippy commented Aug 4, 2017

@brendensoares Did a bigger update to the code base - I moved most of the logger code inside a revel/logger package and broke out the code into separate files. There still is a revel/logger.go file which defines the global loggers but in general it is cleaner. Can you have a quick peek again ?

On controller creation a logger is attached to the controller, this logger has contextual information like the clientIp, request path, etc etc. Every log message sent to this logger will add this to the log message
Added ability to log to json files. If log file ends in json then the logger will write to that file
Added ability to define logger.maxsize (in mega bytes) defaults to 10G
Added ability to define logger.maxage (in days) defaults to 7
If either of those values are exceeded the log file is rotated - ie the old is renamed and a new one is created
Logger implementation is contained inside of the revel/logger package
Added defaults so logging was not discarded (when used by the revel harness)
@notzippy notzippy force-pushed the logger branch 2 times, most recently from 9ff6c36 to b65288f Compare August 11, 2017 06:07
@zuoRambo
Copy link

I think revel.TRACE revel.INFO revel.WARN revel.ERROR should keep.
And seperate controller log and job log is a good idea! 👍

@notzippy
Copy link
Collaborator Author

@zuoRambo thanks for the feedback - The reason we choose to deprecate those variables is because the functionality is contained inside the revel.AppLog, revel.AppLog.Error("message") revel.AppLog.Warn("message") revel.AppLog.Debug("message") revel.AppLog.Info("message") revel.AppLog.Crit("message"). Since they are contained there we should not expose a duplicate method elsewhere.

I realize it may be some work to switch over existing applications to use the new logging, but with the new logging you get some extended features like JSON log records with field/values.

Let me know if you have more reasons for revel.ERROR log messages to remain open

@notzippy notzippy added status-doing Implementation has started and removed waffle: needs review labels Aug 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature New feature request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants