From 74b5e52c4eb54000dcb5a7b46c0977d732bce80f Mon Sep 17 00:00:00 2001 From: Jonathan Date: Thu, 31 Aug 2017 15:59:03 -0400 Subject: PLT-3893: Structured Logging Continues (#7252) * PLT-3893: Imported logger work from https://github.com/MusikPolice/platform * PLT-3893: Integrated logger with system config * PLT-3893: Integrated Mattermost config with logging solution, modified log message serialization so entire message is serialized as a JSON object * PLT-3893: Added support for format strings in Debug methods. Added an overload that does not require a Context object for cases when one isn't available * PLT-3893: Added context and format string support to debug and error methods * PLT-3893: A few updates from pull request feedback * PLT-3893: Changed tests to use testify * Fixed TestAddRemoveConfigListener to no longer assume that there are zero config listeners when the test begins, since other tests could add config listeners * Updated TestGetDeletedChannelsForTeam so that it doesn't assume state when it begins * PLT-3893: Changed File property of log message so that it's relative to /mattermost directory, rather than to wherever the user is running the application from on their machine * Flipped expected/actual assert arguments, added an explicit test for getCallerFilename(...), since it's failing on Jenkins * Added printlns to debug failing tests on Jenkins * Relaxed test cases to avoid failure on Jenkins caused by code coverage calculations. Removed printlns. * Changed the way that caller filename is determined to make it more robust, updated tests to make them more lax, while not choking on the strange paths that Jenkins uses. * Fixed gofmt issues * Added debug output to tests to diagnose Jenkins build failures * Still trying to get some useful debug logging on Jenkins * Changed getCallerFilename to handle the strange paths that runtime.Caller(...) returns on Jenkins * Fixing checkstyle issues --- utils/logger/log4go_json_writer.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 utils/logger/log4go_json_writer.go (limited to 'utils/logger/log4go_json_writer.go') diff --git a/utils/logger/log4go_json_writer.go b/utils/logger/log4go_json_writer.go new file mode 100644 index 000000000..97a8af76b --- /dev/null +++ b/utils/logger/log4go_json_writer.go @@ -0,0 +1,27 @@ +// glue functions that allow logger.go to leverage log4Go to write JSON-formatted log records to a file + +package logger + +import ( + l4g "github.com/alecthomas/log4go" + "github.com/mattermost/platform/utils" +) + +// newJSONLogWriter is a utility method for creating a FileLogWriter set up to +// output JSON record log messages instead of line-based ones. +func newJSONLogWriter(fname string, rotate bool) *l4g.FileLogWriter { + return l4g.NewFileLogWriter(fname, rotate).SetFormat( + `{"level": "%L", + "timestamp": "%D %T", + "source": "%S", + "message": %M + }`).SetRotateLines(utils.LOG_ROTATE_SIZE) +} + +// NewJSONFileLogger - Create a new logger with a "file" filter configured to send JSON-formatted log messages at +// or above lvl to a file with the specified filename. +func NewJSONFileLogger(lvl l4g.Level, filename string) l4g.Logger { + return l4g.Logger{ + "file": &l4g.Filter{Level: lvl, LogWriter: newJSONLogWriter(filename, true)}, + } +} -- cgit v1.2.3-1-g7c22