

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 7
click for more info
Not enough gems
Cost: 6 gems
1: Logging in Go
incomplete
2: Use the Logger
incomplete
3: Logging Requests
incomplete
4: Global Logger vs. Dependency Injection
incomplete
5: Logger Configuration
incomplete
6: Logger Failure
incomplete
7: Buffered Logging
incomplete
8: Logger Cleanup
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
It's more standard to decide what goes to STDERR and what goes to a file based on the environment your application is running in, rather than on separate loggers. Common environments are:
There's no reason a logger can't write to both STDERR and a file at the same time! The io.MultiWriter function takes multiple io.Writer objects and returns a single io.Writer that writes to all of them. For example:
file, err := os.OpenFile(logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
log.Fatalf("failed to open log file: %v", err)
}
multiWriter := io.MultiWriter(os.Stderr, file)
logger := log.New(multiWriter, "INFO: ", log.LstdFlags)
Use one logger that changes output based on LINKO_LOG_FILE.
Assume that in production, Linko has a LINKO_LOG_FILE environment variable set. In local development and staging, it is not set.
If LINKO_LOG_FILE is set, the logger should write to both the file and STDERR. Otherwise, it should only write to STDERR.
Restart your server, setting the LINKO_LOG_FILE environment variable so the tests can verify the file is created:
LINKO_LOG_FILE=linko.access.log go run .
Run and submit the CLI tests from the root of the Linko repo.