Skip to content

Commit

Permalink
Changed logging interface to satisfy print/printf principle. Brakes e…
Browse files Browse the repository at this point in the history
…xisting code.

Before:
  Trace("I:%v", 10) -> "I:10"
Now:
  Trace("I:%v", 10) -> "I:%v10"
Tracef should be used instead of Trace
  • Loading branch information
WiseBird authored and WiseBird committed Aug 29, 2012
1 parent 654409e commit f45367a
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 88 deletions.
18 changes: 7 additions & 11 deletions behavior_asynclogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ const (
type msgQueueItem struct {
level LogLevel
context logContextInterface
format string
params []interface{}
message *logMessage
}

// asyncLogger represents common data for all asynchronous loggers
Expand All @@ -66,10 +65,9 @@ func newAsyncLogger(config *logConfig) *asyncLogger {
func (asnLogger *asyncLogger) innerLog(
level LogLevel,
context logContextInterface,
format string,
params []interface{}) {
message *logMessage) {

asnLogger.addMsgToQueue(level, context, format, params)
asnLogger.addMsgToQueue(level, context, message)
}

func (asnLogger *asyncLogger) Close() {
Expand Down Expand Up @@ -107,16 +105,15 @@ func (asnLogger *asyncLogger) processQueueElement() {
if asnLogger.msgQueue.Len() > 0 {
backElement := asnLogger.msgQueue.Front()
msg, _ := backElement.Value.(msgQueueItem)
asnLogger.processLogMsg(msg.level, msg.format, msg.params, msg.context)
asnLogger.processLogMsg(msg.level, msg.message, msg.context)
asnLogger.msgQueue.Remove(backElement)
}
}

func (asnLogger *asyncLogger) addMsgToQueue(
level LogLevel,
context logContextInterface,
format string,
params []interface{}) {
message *logMessage) {
asnLogger.queueMutex.Lock()
defer asnLogger.queueMutex.Unlock()

Expand All @@ -126,12 +123,11 @@ func (asnLogger *asyncLogger) addMsgToQueue(
asnLogger.flushQueue()
}

param := params
queueItem := msgQueueItem{level, context, format, param}
queueItem := msgQueueItem{level, context, message}
asnLogger.msgQueue.PushBack(queueItem)
asnLogger.queueHasElements.Broadcast()
} else {
err := errors.New(fmt.Sprintf("Queue closed! Cannot process element: %d %s %v", level, format, params))
err := errors.New(fmt.Sprintf("Queue closed! Cannot process element: %d %#v", level, message))
reportInternalError(err)
}
}
5 changes: 2 additions & 3 deletions behavior_synclogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ func newSyncLogger(config *logConfig) (*syncLogger){
func (cLogger *syncLogger) innerLog(
level LogLevel,
context logContextInterface,
format string,
params []interface{}) {
message *logMessage) {

cLogger.processLogMsg(level, format, params, context)
cLogger.processLogMsg(level, message, context)
}

func (syncLogger *syncLogger) Close() {
Expand Down
78 changes: 60 additions & 18 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,46 +162,88 @@ func ReplaceLogger(logger LoggerInterface) error {
return nil
}

// Trace formats message according to format specifier and writes to default logger with log level = Trace
func Trace(format string, params ...interface{}) {
// Tracef formats message according to format specifier and writes to default logger with log level = Trace
func Tracef(format string, params ...interface{}) {
pkgOperationsMutex.Lock()
defer pkgOperationsMutex.Unlock()
Current.traceWithCallDepth(staticFuncCallDepth, format, params)
Current.traceWithCallDepth(staticFuncCallDepth, newFormattedLogMessage(format, params))
}

// Debug formats message according to format specifier and writes to default logger with log level = Debug
func Debug(format string, params ...interface{}) {
// Debugf formats message according to format specifier and writes to default logger with log level = Debug
func Debugf(format string, params ...interface{}) {
pkgOperationsMutex.Lock()
defer pkgOperationsMutex.Unlock()
Current.debugWithCallDepth(staticFuncCallDepth, format, params)
Current.debugWithCallDepth(staticFuncCallDepth, newFormattedLogMessage(format, params))
}

// Info formats message according to format specifier and writes to default logger with log level = Info
func Info(format string, params ...interface{}) {
// Infof formats message according to format specifier and writes to default logger with log level = Info
func Infof(format string, params ...interface{}) {
pkgOperationsMutex.Lock()
defer pkgOperationsMutex.Unlock()
Current.infoWithCallDepth(staticFuncCallDepth, format, params)
Current.infoWithCallDepth(staticFuncCallDepth, newFormattedLogMessage(format, params))
}

// Warn formats message according to format specifier and writes to default logger with log level = Warn
func Warn(format string, params ...interface{}) {
// Warnf formats message according to format specifier and writes to default logger with log level = Warn
func Warnf(format string, params ...interface{}) {
pkgOperationsMutex.Lock()
defer pkgOperationsMutex.Unlock()
Current.warnWithCallDepth(staticFuncCallDepth, format, params)
Current.warnWithCallDepth(staticFuncCallDepth, newFormattedLogMessage(format, params))
}

// Error formats message according to format specifier and writes to default logger with log level = Error
func Error(format string, params ...interface{}) {
// Errorf formats message according to format specifier and writes to default logger with log level = Error
func Errorf(format string, params ...interface{}) {
pkgOperationsMutex.Lock()
defer pkgOperationsMutex.Unlock()
Current.errorWithCallDepth(staticFuncCallDepth, format, params)
Current.errorWithCallDepth(staticFuncCallDepth, newFormattedLogMessage(format, params))
}

// Critical formats message according to format specifier and writes to default logger with log level = Critical
func Critical(format string, params ...interface{}) {
// Criticalf formats message according to format specifier and writes to default logger with log level = Critical
func Criticalf(format string, params ...interface{}) {
pkgOperationsMutex.Lock()
defer pkgOperationsMutex.Unlock()
Current.criticalWithCallDepth(staticFuncCallDepth, format, params)
Current.criticalWithCallDepth(staticFuncCallDepth, newFormattedLogMessage(format, params))
}

// Trace formats message using the default formats for its operands and writes to default logger with log level = Trace
func Trace(v ...interface{}) {
pkgOperationsMutex.Lock()
defer pkgOperationsMutex.Unlock()
Current.traceWithCallDepth(staticFuncCallDepth, newLogMessage(v))
}

// Debug formats message using the default formats for its operands and writes to default logger with log level = Debug
func Debug(v ...interface{}) {
pkgOperationsMutex.Lock()
defer pkgOperationsMutex.Unlock()
Current.debugWithCallDepth(staticFuncCallDepth, newLogMessage(v))
}

// Info formats message using the default formats for its operands and writes to default logger with log level = Info
func Info(v ...interface{}) {
pkgOperationsMutex.Lock()
defer pkgOperationsMutex.Unlock()
Current.infoWithCallDepth(staticFuncCallDepth, newLogMessage(v))
}

// Warn formats message using the default formats for its operands and writes to default logger with log level = Warn
func Warn(v ...interface{}) {
pkgOperationsMutex.Lock()
defer pkgOperationsMutex.Unlock()
Current.warnWithCallDepth(staticFuncCallDepth, newLogMessage(v))
}

// Error formats message using the default formats for its operands and writes to default logger with log level = Error
func Error(v ...interface{}) {
pkgOperationsMutex.Lock()
defer pkgOperationsMutex.Unlock()
Current.errorWithCallDepth(staticFuncCallDepth, newLogMessage(v))
}

// Critical formats message using the default formats for its operands and writes to default logger with log level = Critical
func Critical(v ...interface{}) {
pkgOperationsMutex.Lock()
defer pkgOperationsMutex.Unlock()
Current.criticalWithCallDepth(staticFuncCallDepth, newLogMessage(v))
}

// Flush performs all cleanup, flushes all queued messages, etc. Call this method when your app
Expand Down
Loading

0 comments on commit f45367a

Please sign in to comment.