Documentation
¶
Overview ¶
Package logging provides structured logging for Morphir CLI and tooling.
This package wraps zerolog to provide a consistent logging interface across all Morphir components. It supports:
- Multiple log levels (trace, debug, info, warn, error, fatal)
- Text format (colored console output) and JSON format
- Multi-writer support (stderr + file simultaneously)
- Workspace-aware file logging (relative paths resolve to .morphir/logs/)
- Functional options pattern for configuration
Basic Usage ¶
logger, err := logging.New(
logging.WithLevel("info"),
logging.WithFormat("text"),
)
if err != nil {
// handle error
}
logger.Info().Str("path", "/some/path").Msg("processing file")
logger.Debug().Int("count", 42).Msg("items processed")
File Logging ¶
To enable file logging in addition to stderr:
logger, err := logging.New(
logging.WithLevel("debug"),
logging.WithFile("morphir.log", "/path/to/workspace"),
)
When a relative path is provided and workspaceRoot is set, the log file is created in the workspace's .morphir/logs/ directory.
JSON Format ¶
For log aggregation systems, use JSON format:
logger, err := logging.New(
logging.WithFormat("json"),
)
No-op Logger ¶
For testing or when logging should be disabled:
logger := logging.Noop()
Index ¶
- func LevelString(level zerolog.Level) string
- func LogsDir(workspaceRoot string) string
- func ParseLevel(s string) (zerolog.Level, error)
- func ValidLevels() []string
- type Logger
- func (l Logger) Debug() *zerolog.Event
- func (l Logger) Error() *zerolog.Event
- func (l Logger) Fatal() *zerolog.Event
- func (l Logger) Info() *zerolog.Event
- func (l Logger) Level() zerolog.Level
- func (l Logger) Output(level zerolog.Level) io.Writer
- func (l Logger) Panic() *zerolog.Event
- func (l Logger) SetAsGlobal()
- func (l Logger) SetAsStdLogger()
- func (l Logger) Trace() *zerolog.Event
- func (l Logger) Warn() *zerolog.Event
- func (l Logger) With() zerolog.Context
- func (l Logger) WithLevel(level zerolog.Level) Logger
- func (l Logger) Zerolog() zerolog.Logger
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LevelString ¶
LevelString returns the string representation of a zerolog.Level.
func LogsDir ¶
LogsDir returns the logs directory for a workspace. This is {workspaceRoot}/.morphir/logs/
func ParseLevel ¶
ParseLevel converts a string level name to a zerolog.Level. It supports the following levels (case-insensitive):
- trace: Most verbose, for detailed debugging
- debug: Debugging information
- info: General operational information (default)
- warn/warning: Warning conditions
- error: Error conditions
- fatal: Fatal errors (will call os.Exit)
- panic: Panic conditions (will call panic)
- disabled/off: Disable all logging
An empty string defaults to info level. Returns an error for unrecognized level names.
func ValidLevels ¶
func ValidLevels() []string
ValidLevels returns a slice of all valid level names.
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger wraps zerolog.Logger with Morphir-specific configuration. It provides structured logging with support for multiple outputs, configurable levels, and both text and JSON formats.
Logger is immutable and safe for concurrent use.
func New ¶
New creates a Logger with the given options. Default configuration:
- Level: info
- Format: text (colored console)
- Output: stderr only
Returns an error if the configuration is invalid (e.g., unknown level).
func Noop ¶
func Noop() Logger
Noop returns a disabled logger that discards all output. Useful for testing or when logging should be completely disabled.
func (Logger) Output ¶
Output returns a writer that writes to the logger at the specified level. This is useful for redirecting output from other libraries.
func (Logger) SetAsGlobal ¶
func (l Logger) SetAsGlobal()
SetAsGlobal sets this logger as the global zerolog default. After calling this, zerolog.Log will use this logger's configuration.
func (Logger) SetAsStdLogger ¶
func (l Logger) SetAsStdLogger()
SetAsStdLogger configures the standard library logger to write to this logger. After calling this, log.Print/Printf/Println will write to this logger at info level.
func (Logger) With ¶
With returns a Context for building a child logger with additional fields. The original logger is not modified.
Example:
childLogger := logger.With().Str("component", "validator").Logger()
type Option ¶
type Option func(*config)
Option configures Logger construction.
func WithFile ¶
WithFile enables logging to a file in addition to stderr. If path is relative and workspaceRoot is provided, the file is created in {workspaceRoot}/.morphir/logs/{path}. If path is absolute, it is used as-is. If path is empty, file logging is disabled.
func WithFormat ¶
WithFormat sets the output format. Valid formats: "text" (colored console output) or "json" (structured JSON). Default is "text".
func WithLevel ¶
WithLevel sets the log level. Valid levels: trace, debug, info, warn, error, fatal, disabled. Default is "info".
func WithNoColor ¶
func WithNoColor() Option
WithNoColor disables colored output in text format. This is automatically detected in most cases but can be forced.
func WithStderr ¶
WithStderr overrides the stderr writer. This is primarily useful for testing.