logging

package module
v0.4.0-alpha.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func LevelString

func LevelString(level zerolog.Level) string

LevelString returns the string representation of a zerolog.Level.

func LogsDir

func LogsDir(workspaceRoot string) string

LogsDir returns the logs directory for a workspace. This is {workspaceRoot}/.morphir/logs/

func ParseLevel

func ParseLevel(s string) (zerolog.Level, error)

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

func New(opts ...Option) (Logger, error)

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) Debug

func (l Logger) Debug() *zerolog.Event

Debug logs at debug level.

func (Logger) Error

func (l Logger) Error() *zerolog.Event

Error logs at error level.

func (Logger) Fatal

func (l Logger) Fatal() *zerolog.Event

Fatal logs at fatal level then calls os.Exit(1).

func (Logger) Info

func (l Logger) Info() *zerolog.Event

Info logs at info level.

func (Logger) Level

func (l Logger) Level() zerolog.Level

Level returns the current log level.

func (Logger) Output

func (l Logger) Output(level zerolog.Level) io.Writer

Output returns a writer that writes to the logger at the specified level. This is useful for redirecting output from other libraries.

func (Logger) Panic

func (l Logger) Panic() *zerolog.Event

Panic logs at panic level then panics.

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) Trace

func (l Logger) Trace() *zerolog.Event

Trace logs at trace level (most verbose).

func (Logger) Warn

func (l Logger) Warn() *zerolog.Event

Warn logs at warn level.

func (Logger) With

func (l Logger) With() zerolog.Context

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()

func (Logger) WithLevel

func (l Logger) WithLevel(level zerolog.Level) Logger

WithLevel returns a new Logger with the specified level. The original logger is not modified.

func (Logger) Zerolog

func (l Logger) Zerolog() zerolog.Logger

Zerolog returns the underlying zerolog.Logger. This allows access to advanced zerolog features not exposed by Logger.

type Option

type Option func(*config)

Option configures Logger construction.

func WithFile

func WithFile(path, workspaceRoot string) Option

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

func WithFormat(format string) Option

WithFormat sets the output format. Valid formats: "text" (colored console output) or "json" (structured JSON). Default is "text".

func WithLevel

func WithLevel(level string) Option

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

func WithStderr(w io.Writer) Option

WithStderr overrides the stderr writer. This is primarily useful for testing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL