Skip to content

Commit

Permalink
Merge pull request elastic#245 from ruflin/timestamp-logging
Browse files Browse the repository at this point in the history
Add timestamp to log events.
  • Loading branch information
monicasarbu committed Nov 4, 2015
2 parents ad1a93e + fb1443e commit 8084031
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file based on the
### Added
- Add Console output plugin. #218
- Rename the timestamp field with @timestamp. #237
- Add timestamp to log messages #245

### Improvements
- Send @metadata.beat to Logstash instead of @metadata.index to prevent
Expand Down
54 changes: 26 additions & 28 deletions logp/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,20 @@ func debugMessage(calldepth int, selector, format string, v ...interface{}) {
return
}
}
if _log.toSyslog {
_log.syslog[LOG_INFO].Output(calldepth, fmt.Sprintf(format, v...))
}
if _log.toStderr {
_log.logger.Output(calldepth, fmt.Sprintf("DBG "+format, v...))
}
if _log.toFile {
_log.rotator.WriteLine([]byte(fmt.Sprintf("DBG "+format, v...)))
}

send(calldepth+1, LOG_DEBUG, "DBG ", format, v...)
}
}

func send(calldepth int, level Priority, prefix string, format string, v ...interface{}) {
if _log.toSyslog {
_log.syslog[level].Output(calldepth, fmt.Sprintf(format, v...))
}
if _log.toStderr {
_log.logger.Output(calldepth, fmt.Sprintf(prefix+format, v...))
}
if _log.toFile {
_log.rotator.WriteLine([]byte(fmt.Sprintf(prefix+format, v...)))
}
}

Expand All @@ -73,15 +78,7 @@ func IsDebug(selector string) bool {

func msg(level Priority, prefix string, format string, v ...interface{}) {
if _log.level >= level {
if _log.toSyslog {
_log.syslog[level].Output(3, fmt.Sprintf(format, v...))
}
if _log.toStderr {
_log.logger.Output(3, fmt.Sprintf(prefix+format, v...))
}
if _log.toFile {
_log.rotator.WriteLine([]byte(fmt.Sprintf(prefix+format, v...)))
}
send(4, level, prefix, format, v...)
}
}

Expand Down Expand Up @@ -130,24 +127,20 @@ func LogInit(level Priority, prefix string, toSyslog bool, toStderr bool, debugS
}

if _log.toSyslog {
for prio := LOG_EMERG; prio <= LOG_DEBUG; prio++ {
_log.syslog[prio] = openSyslog(prio, prefix)
if _log.syslog[prio] == nil {
// syslog not available
_log.toSyslog = false
break
}
}
SetToSyslog(true, prefix)
}

if _log.toStderr {
_log.logger = log.New(os.Stderr, prefix, log.Lshortfile)
SetToStderr(true, prefix)
}
}

func SetToStderr(toStderr bool, prefix string) {
_log.toStderr = toStderr
if _log.toStderr {
_log.logger = log.New(os.Stderr, prefix, log.Lshortfile)
// Add timestamp
flag := log.Ldate | log.Ltime | log.Lmicroseconds | log.LUTC | log.Lshortfile
_log.logger = log.New(os.Stderr, prefix, flag)
}
}

Expand All @@ -156,6 +149,11 @@ func SetToSyslog(toSyslog bool, prefix string) {
if _log.toSyslog {
for prio := LOG_EMERG; prio <= LOG_DEBUG; prio++ {
_log.syslog[prio] = openSyslog(prio, prefix)
if _log.syslog[prio] == nil {
// syslog not available
_log.toSyslog = false
break
}
}
}
}
Expand Down

0 comments on commit 8084031

Please sign in to comment.