Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting an output file for logs instead of stdout #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lemon/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type CLI struct {
LineEnding string
LogLevel int
Timeout time.Duration
LogFile string

Help bool

Expand Down
1 change: 1 addition & 0 deletions lemon/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (c *CLI) flags() *flag.FlagSet {
flags.BoolVar(&c.NoFallbackMessages, "no-fallback-messages", false, "Do not show fallback messages")
flags.DurationVar(&c.Timeout, "rpc-timeout", 100*time.Millisecond, "RPC timeout")
flags.IntVar(&c.LogLevel, "log-level", 1, "Log level")
flags.StringVar(&c.LogFile, "log-file", "", "Log output to a file")
return flags
}

Expand Down
1 change: 1 addition & 0 deletions lemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Options:
--trans-loopback=true Translate loopback address [open subcommand only]
--trans-localfile=true Translate local file path [open subcommand only]
--log-level=1 Log level [4 = Critical, 0 = Debug]
--log-file=.lemonlog[.json] Output logs to a custom file instead of stdout
--help Show this message


Expand Down
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"strings"

log "github.com/inconshreveable/log15"

Expand Down Expand Up @@ -39,7 +40,15 @@ func Do(c *lemon.CLI, args []string) int {
}

logLevel := logLevelMap[c.LogLevel]
logger.SetHandler(log.LvlFilterHandler(logLevel, log.StdoutHandler))
logOutHandler := log.StdoutHandler
if len(c.LogFile) > 0 {
logFormat := log.LogfmtFormat()
if strings.HasSuffix(c.LogFile, ".json") {
logFormat = log.JsonFormat()
}
logOutHandler = log.Must.FileHandler(c.LogFile, logFormat)
}
logger.SetHandler(log.LvlFilterHandler(logLevel, logOutHandler))

if c.Help {
fmt.Fprint(c.Err, lemon.Usage)
Expand Down