From 7ac96114c363e64b5ad0d3113315a5357e9d6a44 Mon Sep 17 00:00:00 2001 From: Daniel Oaks Date: Mon, 1 May 2017 18:51:37 +1000 Subject: [PATCH] log: Allow logging to stdout --- CHANGELOG.md | 2 ++ irc/config.go | 2 ++ irc/logger/logger.go | 20 ++++++++++++++------ oragono.go | 1 + oragono.yaml | 1 + 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 509ad542..413ca7b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,10 +9,12 @@ New release of Oragono! ### Config Changes * Added `debug` section containing additional debug settings. +* Added ability to log to `stdout` in logger methods. ### Security ### Added +* Added ability to log to stdout. * Added ability to use StackImpact profiling. ### Changed diff --git a/irc/config.go b/irc/config.go index 7af7532c..a76b4efe 100644 --- a/irc/config.go +++ b/irc/config.go @@ -136,6 +136,7 @@ type ConnectionThrottleConfig struct { // LoggingConfig controls a single logging method. type LoggingConfig struct { Method string + MethodStdout bool MethodStderr bool MethodFile bool Filename string @@ -443,6 +444,7 @@ func LoadConfig(filename string) (config *Config, err error) { return nil, errors.New("Logging configuration specifies 'file' method but 'filename' is empty") } logConfig.MethodFile = methods["file"] + logConfig.MethodStdout = methods["stdout"] logConfig.MethodStderr = methods["stderr"] // levels diff --git a/irc/logger/logger.go b/irc/logger/logger.go index d486ba18..e639ab8c 100644 --- a/irc/logger/logger.go +++ b/irc/logger/logger.go @@ -52,7 +52,7 @@ var ( // Manager is the main interface used to log debug/info/error messages. type Manager struct { loggers []singleLogger - stderrWriteLock sync.Mutex + stdoutWriteLock sync.Mutex // use one lock for both stdout and stderr fileWriteLock sync.Mutex DumpingRawInOut bool } @@ -60,6 +60,7 @@ type Manager struct { // Config represents the configuration of a single logger. type Config struct { // logging methods + MethodStdout bool MethodStderr bool MethodFile bool Filename string @@ -85,6 +86,7 @@ func NewManager(config ...Config) (*Manager, error) { } sLogger := singleLogger{ + MethodSTDOUT: logConfig.MethodStdout, MethodSTDERR: logConfig.MethodStderr, MethodFile: fileMethod{ Enabled: logConfig.MethodFile, @@ -93,7 +95,7 @@ func NewManager(config ...Config) (*Manager, error) { Level: logConfig.Level, Types: typeMap, ExcludedTypes: excludedTypeMap, - stderrWriteLock: &logger.stderrWriteLock, + stdoutWriteLock: &logger.stdoutWriteLock, fileWriteLock: &logger.fileWriteLock, } if typeMap["userinput"] || typeMap["useroutput"] || (typeMap["*"] && !(excludedTypeMap["userinput"] && excludedTypeMap["useroutput"])) { @@ -165,8 +167,9 @@ type fileMethod struct { // singleLogger represents a single logger instance. type singleLogger struct { - stderrWriteLock *sync.Mutex + stdoutWriteLock *sync.Mutex fileWriteLock *sync.Mutex + MethodSTDOUT bool MethodSTDERR bool MethodFile fileMethod Level Level @@ -177,7 +180,7 @@ type singleLogger struct { // Log logs the given message with the given details. func (logger *singleLogger) Log(level Level, logType string, messageParts ...string) { // no logging enabled - if !(logger.MethodSTDERR || logger.MethodFile.Enabled) { + if !(logger.MethodSTDOUT || logger.MethodSTDERR || logger.MethodFile.Enabled) { return } @@ -226,10 +229,15 @@ func (logger *singleLogger) Log(level Level, logType string, messageParts ...str } // output + if logger.MethodSTDOUT { + logger.stdoutWriteLock.Lock() + fmt.Fprintln(colorable.NewColorableStdout(), fullStringFormatted) + logger.stdoutWriteLock.Unlock() + } if logger.MethodSTDERR { - logger.stderrWriteLock.Lock() + logger.stdoutWriteLock.Lock() fmt.Fprintln(colorable.NewColorableStderr(), fullStringFormatted) - logger.stderrWriteLock.Unlock() + logger.stdoutWriteLock.Unlock() } if logger.MethodFile.Enabled { logger.fileWriteLock.Lock() diff --git a/oragono.go b/oragono.go index a19b2e4f..edb5757b 100644 --- a/oragono.go +++ b/oragono.go @@ -49,6 +49,7 @@ Options: var logConfigs []logger.Config for _, lConfig := range config.Logging { logConfigs = append(logConfigs, logger.Config{ + MethodStdout: lConfig.MethodStdout, MethodStderr: lConfig.MethodStderr, MethodFile: lConfig.MethodFile, Filename: lConfig.Filename, diff --git a/oragono.yaml b/oragono.yaml index 80d4cdc0..15c5aebd 100644 --- a/oragono.yaml +++ b/oragono.yaml @@ -206,6 +206,7 @@ logging: # how to log these messages # # file log to given target filename + # stdout log to stdout # stderr log to stderr method: file stderr