Documentation
¶
Overview ¶
Package logger configures slog for AWS CloudWatch
Index ¶
- Constants
- Variables
- func New(opts ...Option) *slog.Logger
- func NewJSONHandler(opts ...Option) slog.Handler
- func NewStdioHandler(opts ...Option) slog.Handler
- type Attributes
- type Option
- func WithLogLevel(level slog.Leveler) Option
- func WithLogLevel7() Option
- func WithLogLevelForMod(mods map[string]slog.Level) Option
- func WithLogLevelForModFromEnv() Option
- func WithLogLevelFromEnv() Option
- func WithLogLevelShorten() Option
- func WithSource() Option
- func WithSourceFileName() Option
- func WithSourceShorten() Option
- func WithTimeFormat(format string) Option
- func WithWriter(w io.Writer) Option
- func WithoutSource() Option
- func WithoutTimestamp() Option
Constants ¶
const ( // EMERGENCY // system is unusable, panic execution of current routine/application, // it is notpossible to gracefully terminate it. EMERGENCY = slog.Level(100) EMR = EMERGENCY // CRITICAL // system is failed, response actions must be taken immediately, // the application is not able to execute correctly but still // able to gracefully exit. CRITICAL = slog.Level(50) CRT = CRITICAL // ERROR // system is failed, unable to recover from error. // The failure do not have global catastrophic impacts but // local functionality is impaired, incorrect result is returned. ERROR = slog.LevelError ERR = ERROR // WARN // system is failed, unable to recover, degraded functionality. // The failure is ignored and application still capable to deliver // incomplete but correct results. WARN = slog.LevelWarn WRN = WARN // NOTICE // system is failed, error is recovered, no impact NOTICE = slog.Level(2) NTC = NOTICE // INFO // output informative status about system INFO = slog.LevelInfo INF = INFO // DEBUG // output debug status about system DEBUG = slog.LevelDebug DEB = DEBUG )
const Version = "v3.2.1"
Variables ¶
var ( // Preset for console logging Console = []Option{ func(o *opts) { o.attributes = append(o.attributes, attrLogLevel7Shorten(true)) }, WithTimeFormat("[15:04:05.000]"), WithLogLevel(NOTICE), WithLogLevelFromEnv(), WithSourceShorten(), WithLogLevelForModFromEnv(), } // Preset for CloudWatch logging CloudWatch = []Option{ WithLogLevelFromEnv(), WithLogLevel7(), WithSourceShorten(), WithoutTimestamp(), WithLogLevelForModFromEnv(), } )
Functions ¶
func NewJSONHandler ¶ added in v3.1.0
JSON logger handler
func NewStdioHandler ¶ added in v3.2.0
Standard I/O handler
Types ¶
type Attributes ¶ added in v3.1.0
Log attributes, forammting combinator
type Option ¶
type Option func(*opts)
The logger config option
func WithLogLevel7 ¶ added in v3.1.0
func WithLogLevel7() Option
WithLevel7 enables from DEBUG to EMERGENCY levels
func WithLogLevelForMod ¶ added in v3.1.0
The logger allows you to define log levels for different modules with flexible granularity. Log levels can be set explicitly using this configuration options.
log.WithLogLevelForMod(map[string]slog.Level{
"github.com/fogfish/logger": log.INFO,
"github.com/you/application": log.DEBUG,
})
The logger uses prefix matching to determine the appropriate log level based on the source code path:
* Per File: A log level defined for a specific file (e.g., github.com/fogfish/logger/logger.go) applies only to that file.
* Per Module: A log level set for a module (e.g., github.com/fogfish/logger) applies to all files within that module.
* Per User/Namespace: A log level defined at a higher level (e.g., github.com/fogfish) applies to all modules under that namespace.
func WithLogLevelForModFromEnv ¶ added in v3.1.0
func WithLogLevelForModFromEnv() Option
func WithLogLevelFromEnv ¶
func WithLogLevelFromEnv() Option
Config Log Level from env CONFIG_LOG_LEVEL, default INFO
export CONFIG_LOG_LEVEL=DEBUG
func WithLogLevelShorten ¶ added in v3.1.0
func WithLogLevelShorten() Option
Config Log Level to be 3 letters only from DEB to EMR
func WithSourceFileName ¶ added in v3.1.0
func WithSourceFileName() Option
Logs file name of the source file only
func WithSourceShorten ¶ added in v3.1.0
func WithSourceShorten() Option
Shorten Source file to few letters only
func WithTimeFormat ¶ added in v3.2.0
Configure the time format
func WithoutTimestamp ¶ added in v3.1.0
func WithoutTimestamp() Option
Exclude timestamp, required by CloudWatch