flash
Types
Collection of typed attributes to add data to loggers.
pub type Attr {
BoolAttr(key: String, value: Bool)
FloatAttr(key: String, value: Float)
GroupAttr(key: String, value: List(Attr))
IntAttr(key: String, value: Int)
StringAttr(key: String, value: String)
}
Constructors
-
BoolAttr(key: String, value: Bool)
-
FloatAttr(key: String, value: Float)
-
GroupAttr(key: String, value: List(Attr))
-
IntAttr(key: String, value: Int)
-
StringAttr(key: String, value: String)
Available log levels, where debug is the lowest level and error is the highest level.
pub type Level {
DebugLevel
InfoLevel
WarnLevel
ErrorLevel
}
Constructors
-
DebugLevel
-
InfoLevel
-
WarnLevel
-
ErrorLevel
pub type Logger {
Logger(
level: Level,
writer: Writer,
parent: Option(Logger),
group: String,
attrs: List(Attr),
)
}
Constructors
-
Logger( level: Level, writer: Writer, parent: Option(Logger), group: String, attrs: List(Attr), )
Constants
Functions
pub fn debug(logger: Logger, message: String) -> Nil
Logs the message and any attributes at the debug level.
pub fn enabled(logger: Logger, level: Level) -> Bool
Determines if the logger is enabled for the given log level.
pub fn error(logger: Logger, message: String) -> Nil
Logs the message and any attributes at the error level.
pub fn info(logger: Logger, message: String) -> Nil
Logs the message and any attributes at the info level.
pub fn json_writer(
level: Level,
message: String,
attrs: List(Attr),
) -> Nil
A writer that writes to standard out using a JSON representation. Here’s some example code and the associated JSON output:
let logger = with_attr(logger, StringAttr("request_id", "foobar"))
info(logger, "/health")
{"level":"info","time":"2024-03-09T12:52:43.657-05:00","message":"/health","request_id":"foobar"}
The level
, time
, and message
attributes are added automatically and will be
ordered before other attributes. Other attributes are sorted lexicographically with
groups being sorted after non group attributes. If multiple attributes in the same
group share a key, the last attribute with the key is chosen.
pub fn level_to_string(level: Level) -> String
Converts a level variant to an equivalent string representation.
pub fn log(logger: Logger, level: Level, message: String) -> Nil
Logs the message and any attributes if the logger is enabled to log at the given log level.
pub fn new(
level: Level,
writer: fn(Level, String, List(Attr)) -> Nil,
) -> Logger
Creates a logger that logs for levels greater than or equal to the given level and writes using the given writer.
pub fn parse_level(level: String) -> Result(Level, Nil)
Parses a string representation of a level to the equivalent level variant.
pub fn text_writer(
level: Level,
message: String,
attrs: List(Attr),
) -> Nil
A writer that writes to standard out using a text representation. Here’s some example code and the associated text output:
let logger = with_attr(logger, StringAttr("request_id", "foobar"))
info(logger, "/health")
12:59:37 INFO /health request_id=foobar
Attributes are sorted lexicographically with groups being sorted after non group attributes. If multiple attributes in the same group share a key, the last attribute with the key is chosen.
pub fn warn(logger: Logger, message: String) -> Nil
Logs the message and any attributes at the warn level.
pub fn with_attr(logger: Logger, attr: Attr) -> Logger
Adds the given attribute to the logger.
pub fn with_attrs(logger: Logger, attrs: List(Attr)) -> Logger
Adds the list of attributes to the logger.
pub fn with_group(logger: Logger, group: String) -> Logger
Creates a child group from the given logger with the given group name.