How mishankov/logman works
Main Components
• Logger Core (logman.go): This is the central API that instantiates loggers using provided components.
• Loggers (in the “loggers” directory): Implements default logger behavior (e.g., NewDefaultLogger) that wraps the core functionality.
• Formatters (in the “formatters” directory): Contains multiple implementations for formatting log messages. This includes:
• DefaultFormatter – converts messages to a human-readable string.
• JSONFormatter – outputs logs in JSON format.
• Context-Aware Formatters – such as DefaultContextFormatter and JSONContextFormatter (which extract values from context).
• Filters (in the “filters” directory): Currently provides at least a LevelFilter that filters out messages based on log level.
• Writers (in the “writers” directory): Responsible for sending output to different destinations (e.g., a file writer that outputs logs to a file).
• Supporting files: The example_test.go and logman_test.go files ensure that TDD practices are followed, and CI/CD setups (in the .github folder) are maintained for quality.
Relationships and Interactions
• User Code/Application: It uses the Logman API to perform logging. When a log request is made (e.g., logger.Info or logger.Debug), the logger:
• Passes the log message (along with context, if applicable) to a Formatter.
• The Formatter processes the message into a final string (or JSON) representation.
• Before output, a Filter (like the LevelFilter) might check if the message should be logged.
• Finally, the processed message is forwarded to a Writer which delivers the log to its destination (stdout or file).
• Composition: The “custom logger” example in the README shows how different components (writer, formatter, and filter) are injected into the logger instance via the core API.
Architectural Patterns and Design Principles
• Component-Based Design: Each module (logger, formatter, filter, writer) performs a specific, isolated function, and the logger composes them together.
• Dependency Injection: The logger accepts a formatter, writer, and optionally a filter, allowing flexibility for different use cases.
• Extensibility: The design allows developers to create custom implementations to replace any of the built-in ones.
• Test-Driven Development (TDD): Evident from the test files, ensuring each component is well-tested.