Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Adding log coloring command line option parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
obourdon committed Jun 20, 2016
1 parent 91fc4c2 commit c6a7c45
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/configs/snap-config-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"log_level": 2,
"log_path": "/some/log/dir",
"log_truncate": false,
"log_colors": true,
"gomaxprocs": 2,
"control": {
"auto_discover_path": "/some/directory/with/plugins",
Expand Down
5 changes: 5 additions & 0 deletions examples/configs/snap-config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ log_path: /some/log/dir
# true => truncate
log_truncate: false

# log_colors specifies if log file output is colorified
# true => colors
# false => no colors
log_colors: true

# Gomaxprocs sets the number of cores to use on the system
# for snapd to use. Default for gomaxprocs is 1
gomaxprocs: 2
Expand Down
28 changes: 28 additions & 0 deletions snapd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ var (
Name: "log-truncate",
Usage: "Log file truncating mode. Default is false => append (true => truncate).",
}
flLogColors = cli.BoolTFlag{
Name: "log-colors",
Usage: "Log file coloring mode. Default is true => colored (--log-colors=false => no colors).",
}
flLogLevel = cli.IntFlag{
Name: "log-level, l",
Usage: "1-5 (Debug, Info, Warning, Error, Fatal)",
Expand Down Expand Up @@ -101,6 +105,7 @@ const (
defaultGoMaxProcs int = 1
defaultLogPath string = ""
defaultLogTruncate bool = false
defaultLogColors bool = true
defaultConfigPath string = "/etc/snap/snapd.conf"
)

Expand All @@ -113,6 +118,7 @@ type Config struct {
GoMaxProcs int `json:"gomaxprocs,omitempty"yaml:"gomaxprocs,omitempty"`
LogPath string `json:"log_path,omitempty"yaml:"log_path,omitempty"`
LogTruncate bool `json:"log_truncate,omitempty"yaml:"log_truncate,omitempty"`
LogColors bool `json:"log_colors,omitempty"yaml:"log_colors,omitempty"`
Control *control.Config `json:"control,omitempty"yaml:"control,omitempty"`
Scheduler *scheduler.Config `json:"scheduler,omitempty"yaml:"scheduler,omitempty"`
RestAPI *rest.Config `json:"restapi,omitempty"yaml:"restapi,omitempty"`
Expand All @@ -139,6 +145,10 @@ const (
"description": "truncate log file default is false",
"type": "boolean"
},
"log_colors": {
"description": "log file colored output default is true",
"type": "boolean"
},
"gomaxprocs": {
"description": "value to be used for gomaxprocs",
"type": "integer",
Expand Down Expand Up @@ -190,6 +200,7 @@ func main() {
flLogLevel,
flLogPath,
flLogTruncate,
flLogColors,
flMaxProcs,
flConfig,
}
Expand Down Expand Up @@ -249,6 +260,17 @@ func action(ctx *cli.Context) {
defer file.Close()
log.SetOutput(file)
}
// Because even though github.com/Sirupsen/logrus states that
// 'Logs the event in colors if stdout is a tty, otherwise without colors'
// Seems like this does not work
// Please note however that the default output format without colors is somewhat different (timestamps, ...)
//
// We could also restrict this command line parameter to only apply when no logpath is given
// and forcing the coloring to off when using a file but this might not please users who like to use
// redirect mechanisms like # snapd -t 0 -l 1 2>&1 | tee my.log
if !cfg.LogColors {
log.SetFormatter(&log.TextFormatter{DisableColors: true})
}

// Validate log level and trust level settings for snapd
validateLevelSettings(cfg.LogLevel, cfg.Control.PluginTrust)
Expand Down Expand Up @@ -453,6 +475,7 @@ func getDefaultConfig() *Config {
GoMaxProcs: defaultGoMaxProcs,
LogPath: defaultLogPath,
LogTruncate: defaultLogTruncate,
LogColors: defaultLogColors,
Control: control.GetDefaultConfig(),
Scheduler: scheduler.GetDefaultConfig(),
RestAPI: rest.GetDefaultConfig(),
Expand Down Expand Up @@ -576,6 +599,7 @@ func applyCmdLineFlags(cfg *Config, ctx *cli.Context) {
cfg.LogLevel = setIntVal(cfg.LogLevel, ctx, "log-level")
cfg.LogPath = setStringVal(cfg.LogPath, ctx, "log-path")
cfg.LogTruncate = setBoolVal(cfg.LogTruncate, ctx, "log-truncate")
cfg.LogColors = setBoolVal(cfg.LogColors, ctx, "log-colors")
// next for the flags related to the control package
cfg.Control.MaxRunningPlugins = setIntVal(cfg.Control.MaxRunningPlugins, ctx, "max-running-plugins")
cfg.Control.PluginTrust = setIntVal(cfg.Control.PluginTrust, ctx, "plugin-trust")
Expand Down Expand Up @@ -700,6 +724,10 @@ func (c *Config) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(v, &(c.LogTruncate)); err != nil {
return fmt.Errorf("%v (while parsing 'log_truncate')", err)
}
case "log_colors":
if err := json.Unmarshal(v, &(c.LogColors)); err != nil {
return fmt.Errorf("%v (while parsing 'log_colors')", err)
}
case "control":
if err := json.Unmarshal(v, c.Control); err != nil {
return err
Expand Down

0 comments on commit c6a7c45

Please sign in to comment.