github.com/blend/go-sdk@v1.20220411.3/logger/config.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package logger
     9  
    10  import (
    11  	"context"
    12  	"strings"
    13  
    14  	"github.com/blend/go-sdk/env"
    15  )
    16  
    17  // Config is the logger config.
    18  type Config struct {
    19  	// Flags hold the event types (i.e. flags) that are enabled.
    20  	// If a flag is disabled, it is hidden from output _and_ listeners are not triggered.
    21  	Flags []string `json:"flags,omitempty" yaml:"flags,omitempty" env:"LOG_FLAGS,csv"`
    22  	// Scopes hold the scope paths that are enabled.
    23  	// If a scope is disabled, any events for that scope (or logger path) are hidden from output and listeners are not triggered.
    24  	// A scope or path can be set on a logger with `sub := log.WithPath("foo", "bar")`.
    25  	// It defaults to all scopes being enabled, or `*`.
    26  	Scopes []string `json:"scopes,omitempty" yaml:"scopes,omitempty" env:"LOG_SCOPES,csv"`
    27  	// Writable holds event types (i.e. flags) that are shown in output.
    28  	// If a flag is not writable, it is hidden from output but listeners _are_ triggered.
    29  	// It defaults to all flags being writable, or `all`.
    30  	Writable []string `json:"writable,omitempty" yaml:"writable,omitempty" env:"LOG_WRITABLE,csv"`
    31  	// WritableScopes are scopes that are shown in in output.
    32  	// A scope can be set on a logger with `sub := log.WithPath("foo", "bar")`.
    33  	// If a scope is not writable, it is hidden from output but listeners _are_ triggered.
    34  	// It defaults to all scopes being writable, or `*`.
    35  	WritableScopes []string `json:"writableScopes,omitempty" yaml:"writableScopes,omitempty" env:"LOG_WRITABLE_SCOPES,csv"`
    36  	// Format is the output format, either `text` or `json`.
    37  	Format string `json:"format,omitempty" yaml:"format,omitempty" env:"LOG_FORMAT"`
    38  	// Text holds text output specific options.
    39  	Text TextConfig `json:"text,omitempty" yaml:"text,omitempty"`
    40  	// JSON holds json specific options.
    41  	JSON JSONConfig `json:"json,omitempty" yaml:"json,omitempty"`
    42  }
    43  
    44  // Resolve resolves the config.
    45  func (c *Config) Resolve(ctx context.Context) error {
    46  	return env.GetVars(ctx).ReadInto(c)
    47  }
    48  
    49  // FlagsOrDefault returns the enabled logger events.
    50  func (c Config) FlagsOrDefault() []string {
    51  	if len(c.Flags) > 0 {
    52  		return c.Flags
    53  	}
    54  	return DefaultFlags
    55  }
    56  
    57  // WritableOrDefault returns the enabled logger events.
    58  func (c Config) WritableOrDefault() []string {
    59  	if len(c.Writable) > 0 {
    60  		return c.Writable
    61  	}
    62  	return DefaultFlagsWritable
    63  }
    64  
    65  // ScopesOrDefault returns the enabled logger scopes.
    66  func (c Config) ScopesOrDefault() []string {
    67  	if len(c.Scopes) > 0 {
    68  		return c.Scopes
    69  	}
    70  	return DefaultScopes
    71  }
    72  
    73  // WritableScopesOrDefault returns the writable logger scopes.
    74  func (c Config) WritableScopesOrDefault() []string {
    75  	if len(c.WritableScopes) > 0 {
    76  		return c.WritableScopes
    77  	}
    78  	return DefaultWritableScopes
    79  }
    80  
    81  // FormatOrDefault returns the output format or a default.
    82  func (c Config) FormatOrDefault() string {
    83  	if c.Format != "" {
    84  		return c.Format
    85  	}
    86  	return FormatText
    87  }
    88  
    89  // Formatter returns the configured writers
    90  func (c Config) Formatter() WriteFormatter {
    91  	switch strings.ToLower(string(c.FormatOrDefault())) {
    92  	case FormatJSON:
    93  		return NewJSONOutputFormatter(OptJSONConfig(c.JSON))
    94  	case FormatText:
    95  		return NewTextOutputFormatter(OptTextConfig(c.Text))
    96  	default:
    97  		return NewTextOutputFormatter(OptTextConfig(c.Text))
    98  	}
    99  }
   100  
   101  // TextConfig is the config for a text formatter.
   102  type TextConfig struct {
   103  	HideTimestamp bool   `json:"hideTimestamp,omitempty" yaml:"hideTimestamp,omitempty" env:"LOG_HIDE_TIMESTAMP"`
   104  	HideFields    bool   `json:"hideFields,omitempty" yaml:"hideFields,omitempty" env:"LOG_HIDE_FIELDS"`
   105  	NoColor       bool   `json:"noColor,omitempty" yaml:"noColor,omitempty" env:"NO_COLOR"`
   106  	TimeFormat    string `json:"timeFormat,omitempty" yaml:"timeFormat,omitempty" env:"LOG_TIME_FORMAT"`
   107  }
   108  
   109  // TimeFormatOrDefault returns a field value or a default.
   110  func (twc TextConfig) TimeFormatOrDefault() string {
   111  	if len(twc.TimeFormat) > 0 {
   112  		return twc.TimeFormat
   113  	}
   114  	return DefaultTextTimeFormat
   115  }
   116  
   117  // JSONConfig is the config for a json formatter.
   118  type JSONConfig struct {
   119  	Pretty       bool   `json:"pretty,omitempty" yaml:"pretty,omitempty" env:"LOG_JSON_PRETTY"`
   120  	PrettyPrefix string `json:"prettyPrefix,omitempty" yaml:"prettyPrefix,omitempty" env:"LOG_JSON_PRETTY_PREFIX"`
   121  	PrettyIndent string `json:"prettyIndent,omitempty" yaml:"prettyIndent,omitempty" env:"LOG_JSON_PRETTY_INDENT"`
   122  }
   123  
   124  // PrettyPrefixOrDefault returns the pretty prefix or a default.
   125  func (jc JSONConfig) PrettyPrefixOrDefault() string {
   126  	return jc.PrettyPrefix
   127  }
   128  
   129  // PrettyIndentOrDefault returns the pretty indent or a default.
   130  func (jc JSONConfig) PrettyIndentOrDefault() string {
   131  	if jc.PrettyIndent != "" {
   132  		return jc.PrettyIndent
   133  	}
   134  	return "  "
   135  }