Skip to content

Commit

Permalink
fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Jul 26, 2024
1 parent ec6e218 commit d641816
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 34 deletions.
30 changes: 17 additions & 13 deletions examples/custom.mojo
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from stump import (
DEBUG,
DEFAULT_FORMAT,
Processor,
Context,
Styles,
Expand All @@ -9,7 +8,6 @@ from stump import (
PrintLogger,
add_log_level,
add_timestamp,
add_timestamp_with_format,
)
import external.mist

Expand All @@ -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()
Expand All @@ -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,
)
Expand All @@ -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(),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/json.mojo
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
4 changes: 2 additions & 2 deletions examples/logfmt.mojo
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
1 change: 0 additions & 1 deletion examples/message_only.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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](),
)

Expand Down
4 changes: 2 additions & 2 deletions examples/turn_off_styler.mojo
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
2 changes: 1 addition & 1 deletion stump/__init__.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
26 changes: 13 additions & 13 deletions stump/processor.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down

0 comments on commit d641816

Please sign in to comment.