-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tracer is now initialized with an internal logger implementation if ELASTIC_APM_LOG_FILE is specified. Without setting this environment variable, logging is disabled as before. Setting ELASTIC_APM_LOG_FILE to stdout or stderr will cause logging to write to stdout/stderr respectively. Otherwise, specifying a file path will cause that file to be created/truncated and written to. By default, only errors will be logged. Setting ELASTIC_APM_LOG_LEVEL to "debug" will cause debug messages to be printed in addition.
- Loading branch information
Showing
5 changed files
with
119 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package apmlog | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"os" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
var ( | ||
// DefaultLogger is the default Logger to use, if ELASTIC_APM_LOG_* are specified. | ||
DefaultLogger Logger | ||
) | ||
|
||
func init() { | ||
fileStr := strings.TrimSpace(os.Getenv("ELASTIC_APM_LOG_FILE")) | ||
if fileStr == "" { | ||
return | ||
} | ||
|
||
var logWriter io.Writer | ||
switch strings.ToLower(fileStr) { | ||
case "stdout": | ||
logWriter = os.Stdout | ||
case "stderr": | ||
logWriter = os.Stderr | ||
default: | ||
f, err := os.Create(fileStr) | ||
if err != nil { | ||
log.Printf("failed to create %q: %s (disabling logging)", fileStr, err) | ||
return | ||
} | ||
logWriter = &syncFile{File: f} | ||
} | ||
|
||
const defaultLevel = zerolog.ErrorLevel | ||
logger := zerolog.New(logWriter) | ||
if levelStr := strings.TrimSpace(os.Getenv("ELASTIC_APM_LOG_LEVEL")); levelStr != "" { | ||
level, err := zerolog.ParseLevel(strings.ToLower(levelStr)) | ||
if err != nil { | ||
log.Printf("invalid ELASTIC_APM_LOG_LEVEL %q, falling back to %q", levelStr, defaultLevel) | ||
level = defaultLevel | ||
} | ||
logger = logger.Level(level) | ||
} else { | ||
logger = logger.Level(defaultLevel) | ||
} | ||
DefaultLogger = zerologLogger{l: logger} | ||
} | ||
|
||
// Logger provides methods for logging. | ||
type Logger interface { | ||
Debugf(format string, args ...interface{}) | ||
Errorf(format string, args ...interface{}) | ||
} | ||
|
||
type zerologLogger struct { | ||
l zerolog.Logger | ||
} | ||
|
||
// Debugf logs a message with log.Printf, with a DEBUG prefix. | ||
func (l zerologLogger) Debugf(format string, args ...interface{}) { | ||
l.l.Debug().Timestamp().Msgf(format, args...) | ||
} | ||
|
||
// Errorf logs a message with log.Printf, with an ERROR prefix. | ||
func (l zerologLogger) Errorf(format string, args ...interface{}) { | ||
l.l.Error().Timestamp().Msgf(format, args...) | ||
} | ||
|
||
type syncFile struct { | ||
mu sync.Mutex | ||
*os.File | ||
} | ||
|
||
// Write calls f.File.Write with f.mu held, to protect multiple Tracers | ||
// in the same process from one another. | ||
func (f *syncFile) Write(data []byte) (int, error) { | ||
f.mu.Lock() | ||
defer f.mu.Unlock() | ||
return f.File.Write(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters