From d641816471e52f817a96ff39f2a71809de92edab Mon Sep 17 00:00:00 2001 From: Mikhail Tavarez Date: Fri, 26 Jul 2024 15:03:54 -0500 Subject: [PATCH] fix examples --- examples/custom.mojo | 30 +++++++++++++++++------------- examples/json.mojo | 4 ++-- examples/logfmt.mojo | 4 ++-- examples/message_only.mojo | 1 - examples/turn_off_styler.mojo | 4 ++-- stump/__init__.mojo | 2 +- stump/processor.mojo | 26 +++++++++++++------------- 7 files changed, 37 insertions(+), 34 deletions(-) diff --git a/examples/custom.mojo b/examples/custom.mojo index 687e928..19ca70f 100644 --- a/examples/custom.mojo +++ b/examples/custom.mojo @@ -1,6 +1,5 @@ from stump import ( DEBUG, - DEFAULT_FORMAT, Processor, Context, Styles, @@ -9,7 +8,6 @@ from stump import ( PrintLogger, add_log_level, add_timestamp, - add_timestamp_with_format, ) import external.mist @@ -23,18 +21,22 @@ fn add_my_name(context: Context, level: String) -> Context: # Define custom processors to add extra information to the log output. fn my_processors() -> List[Processor]: - return List[Processor](add_log_level, add_timestamp_with_format["YYYY"](), add_my_name) + return List[Processor](add_log_level, add_timestamp, add_my_name) # Define custom styles to format and colorize the log output. fn my_styles() -> Styles: # Log level styles, by default just set colors - var levels = Sections() - levels["FATAL"] = mist.Style().background(0xD4317D) - levels["ERROR"] = mist.Style().background(0xD48244) - levels["INFO"] = mist.Style().background(0x13ED84) - levels["WARN"] = mist.Style().background(0xDECF2F) - levels["DEBUG"] = mist.Style().background(0xBD37DB) + var base_style = mist.Style() + var faint_style = mist.Style().faint() + + var levels = List[mist.Style]( + base_style.background(0xD4317D), + base_style.background(0xD48244), + base_style.background(0x13ED84), + base_style.background(0xDECF2F), + base_style.background(0xBD37DB), + ) var keys = Sections() keys["name"] = mist.Style().foreground(0xC9A0DC).underline() @@ -43,9 +45,12 @@ fn my_styles() -> Styles: values["name"] = mist.Style().foreground(0xD48244).bold() return Styles( + timestamp=base_style, + message=base_style, + key=faint_style, + value=base_style, + separator=faint_style, levels=levels, - key=mist.Style().faint(), - separator=mist.Style().faint(), keys=keys, values=values, ) @@ -55,9 +60,8 @@ fn my_styles() -> Styles: alias LOG_LEVEL = DEBUG # Build a bound logger with custom processors and styling -alias logger = BoundLogger( +var logger = BoundLogger( PrintLogger(LOG_LEVEL), - formatter=DEFAULT_FORMAT, processors=my_processors(), styles=my_styles(), ) diff --git a/examples/json.mojo b/examples/json.mojo index 1e287b4..ad78de7 100644 --- a/examples/json.mojo +++ b/examples/json.mojo @@ -1,9 +1,9 @@ -from stump import DEBUG, JSON_FORMAT, BoundLogger, PrintLogger +from stump import DEBUG, json_formatter, BoundLogger, PrintLogger # The loggers are compiled at build time, so we can reuse it. alias LOG_LEVEL = DEBUG -var logger = BoundLogger(PrintLogger(LOG_LEVEL), formatter=JSON_FORMAT) +var logger = BoundLogger(PrintLogger(LOG_LEVEL), formatter=json_formatter, apply_styles=False) fn main(): diff --git a/examples/logfmt.mojo b/examples/logfmt.mojo index 1bf04a2..33cafe1 100644 --- a/examples/logfmt.mojo +++ b/examples/logfmt.mojo @@ -1,9 +1,9 @@ -from stump import DEBUG, LOGFMT_FORMAT, BoundLogger, PrintLogger +from stump import DEBUG, logfmt_formatter, BoundLogger, PrintLogger # The loggers are compiled at build time, so we can reuse it. alias LOG_LEVEL = DEBUG -var logger = BoundLogger(PrintLogger(LOG_LEVEL), formatter=LOGFMT_FORMAT) +var logger = BoundLogger(PrintLogger(LOG_LEVEL), formatter=logfmt_formatter, apply_styles=False) fn main(): diff --git a/examples/message_only.mojo b/examples/message_only.mojo index 7b55e44..575c887 100644 --- a/examples/message_only.mojo +++ b/examples/message_only.mojo @@ -16,7 +16,6 @@ alias LOG_LEVEL = DEBUG # Build a bound logger with custom processors and styling var logger = BoundLogger( PrintLogger(LOG_LEVEL), - formatter=DEFAULT_FORMAT, processors=List[Processor](), ) diff --git a/examples/turn_off_styler.mojo b/examples/turn_off_styler.mojo index c14a78e..33cafe1 100644 --- a/examples/turn_off_styler.mojo +++ b/examples/turn_off_styler.mojo @@ -1,9 +1,9 @@ -from stump import DEBUG, LOGFMT_FORMAT, BoundLogger, PrintLogger +from stump import DEBUG, logfmt_formatter, BoundLogger, PrintLogger # The loggers are compiled at build time, so we can reuse it. alias LOG_LEVEL = DEBUG -var logger = BoundLogger(PrintLogger(LOG_LEVEL), formatter=LOGFMT_FORMAT, apply_styles=False) +var logger = BoundLogger(PrintLogger(LOG_LEVEL), formatter=logfmt_formatter, apply_styles=False) fn main(): diff --git a/stump/__init__.mojo b/stump/__init__.mojo index 726e81a..e66af29 100644 --- a/stump/__init__.mojo +++ b/stump/__init__.mojo @@ -4,7 +4,7 @@ from .logger import FileLogger, STDLogger, PrintLogger, Logger from .processor import ( add_log_level, add_timestamp, - add_timestamp_with_format, + # add_timestamp_with_format, get_default_processors, Processor, ) diff --git a/stump/processor.mojo b/stump/processor.mojo index 8a9f3d5..fa6d386 100644 --- a/stump/processor.mojo +++ b/stump/processor.mojo @@ -38,23 +38,23 @@ fn add_log_level(context: Context, level: String) -> Context: return new_context -# If you need to modify something within the processor function, create a function that returns a Processor -fn add_timestamp_with_format[format: String]() -> Processor: - """Adds a timestamp to the log message with the specified format. - The format should be a valid format string for Morrow.now().format() or "iso". +# # If you need to modify something within the processor function, create a function that returns a Processor +# fn add_timestamp_with_format[format: String]() -> Processor: +# """Adds a timestamp to the log message with the specified format. +# The format should be a valid format string for Morrow.now().format() or "iso". - The default format for timestamps is `YYYY-MM-DD HH:mm:ss`. +# The default format for timestamps is `YYYY-MM-DD HH:mm:ss`. - Params: - format: The format string for the timestamp. - """ +# Params: +# format: The format string for the timestamp. +# """ - fn processor(context: Context, level: String) -> Context: - var new_context = context - new_context["timestamp"] = DateT.now().strftime(format) - return new_context +# fn processor(context: Context, level: String) -> Context: +# var new_context = context +# new_context["timestamp"] = DateT.now().strftime(format) +# return new_context - return processor +# return processor fn get_default_processors() -> List[Processor]: