Firehose: Flexible Logging Library

Hi everyone,

A month ago I made a basic logging library: GitHub - josiahls/firehose for another package I’m developing that needed better logging.

This supports plugin:

  • outputs, filters, and formatters similar to the python logging library.

Caveats:

  • Hobby project of a hobby project
  • Custom filters, outputs, and formatters need to be directly added to the library itself in each module’s init file at: firehose/__init__.mojo. Ideally we would just have a filter register function, or skip the need for variants altogether.
  • Duplicates the Variant struct for each module type to get union-like behavior. Ideas to get around this are welcome.
  • Not optimized for performance. I currently update it as the need arises.

Example use case

from firehose.logging import Logger
from firehose import (
    DefaultLoggerFilter, DefaultLoggerFormatter, DefaultLoggerOutputer
)


fn main() raises:
    """Just run the logs and print them to visually validate the format."""
    var logger = Logger("test", "INFO")
    # Test all of the format fields
    logger.add_formatter_copy(
        DefaultLoggerFormatter(
            '%(file)s:%(file_name)s:%(line)s:%(column)s %(logger_name)s - %(message_level)s - %(message_level_name)s - %(message_level_name_short)s: %(original_message)s'
        )
    )
    logger.add_outputter_copy(DefaultLoggerOutputer())
    logger.add_filter_copy(DefaultLoggerFilter()) # Default is INFO level

    logger.trace("Test trace message")
    logger.debug("Test debug message")
    logger.info("Test message")
    logger.warning("Test warning message")
    logger.error("Test error message")
    logger.critical("Test critical message")

1 Like

This is great! It’s nice to have a simple logging framework that’s a bit more powerful than what the stdlib offers. As you know, the stdlib version is still work in progress, so do you have any feedback on that?

I’d also like to make a simple feature request, could you make it so that we can do something like MOJO_LOG=info ./mojo_program for an already compiled mojo program, and it will switch the log level? In other languages, I often find myself wanting to default to info, but then I see some weird behavior and want to increase the verbosity without having to do a recompile.

Added env var level adjustment: Adds env var log level adjustment support by josiahls · Pull Request #1 · josiahls/firehose · GitHub

I think my general feedback is basically support similar capabilities as python e.g.:

  • custom outputs, filtering, formatting

More controversial:

  • add a trace level. Generally debug is super verbose output but still readable, trace is super noisy logs usually inside a loop and is a last resort for “why is this part hanging?”
1 Like

I’m very much in favor of a trace level, since that’s where framework or infrastructure-level logs like “scheduling this async task” need to go for when you’re having a really, really bad day.

Thanks for the quick turnaround!

Great work! You could have named it firewood.

Great work! I also worked on a logger library that’s been languishing for the past few releases, so it’s awesome to see one being realized :slight_smile: