Files
BitLogger/bitlogger/config.mbt
T
2026-05-10 15:06:20 +08:00

1172 lines
33 KiB
MoonBit

pub(all) suberror ConfigError {
InvalidConfig(String)
}
pub(all) enum SinkKind {
Console
JsonConsole
TextConsole
File
}
pub struct TextFormatterConfig {
show_timestamp : Bool
show_level : Bool
show_target : Bool
show_fields : Bool
separator : String
field_separator : String
template : String
color_mode : ColorMode
style_markup : StyleMarkupMode
style_tags : Map[String, TextStyle]
}
pub fn TextFormatterConfig::new(
show_timestamp~ : Bool = true,
show_level~ : Bool = true,
show_target~ : Bool = true,
show_fields~ : Bool = true,
separator~ : String = " ",
field_separator~ : String = " ",
template~ : String = "",
color_mode~ : ColorMode = ColorMode::Never,
style_markup~ : StyleMarkupMode = StyleMarkupMode::Full,
style_tags~ : Map[String, TextStyle] = {},
) -> TextFormatterConfig {
{
show_timestamp,
show_level,
show_target,
show_fields,
separator,
field_separator,
template,
color_mode,
style_markup,
style_tags,
}
}
fn style_tag_registry_from_config(style_tags : Map[String, TextStyle]) -> StyleTagRegistry {
let registry = style_tag_registry()
for name, style in style_tags {
ignore(registry.set_tag(name, style=style))
}
registry
}
pub fn TextFormatterConfig::to_formatter(self : TextFormatterConfig) -> TextFormatter {
text_formatter(
show_timestamp=self.show_timestamp,
show_level=self.show_level,
show_target=self.show_target,
show_fields=self.show_fields,
separator=self.separator,
field_separator=self.field_separator,
template=self.template,
color_mode=self.color_mode,
style_markup=self.style_markup,
style_tags=if self.style_tags.length() == 0 {
None
} else {
Some(style_tag_registry_from_config(self.style_tags))
},
)
}
pub struct QueueConfig {
max_pending : Int
overflow : QueueOverflowPolicy
}
pub fn QueueConfig::new(
max_pending : Int,
overflow~ : QueueOverflowPolicy = QueueOverflowPolicy::DropNewest,
) -> QueueConfig {
{ max_pending, overflow }
}
pub struct SinkConfig {
kind : SinkKind
path : String
append : Bool
auto_flush : Bool
rotation : FileRotation?
text_formatter : TextFormatterConfig
}
pub fn SinkConfig::new(
kind~ : SinkKind = SinkKind::Console,
path~ : String = "",
append~ : Bool = true,
auto_flush~ : Bool = true,
rotation~ : FileRotation? = None,
text_formatter~ : TextFormatterConfig = default_text_formatter_config(),
) -> SinkConfig {
{
kind,
path,
append,
auto_flush,
rotation,
text_formatter,
}
}
pub struct LoggerConfig {
min_level : Level
target : String
timestamp : Bool
sink : SinkConfig
queue : QueueConfig?
}
pub fn LoggerConfig::new(
min_level~ : Level = Level::Info,
target~ : String = "",
timestamp~ : Bool = false,
sink~ : SinkConfig = default_sink_config(),
queue~ : QueueConfig? = None,
) -> LoggerConfig {
{
min_level,
target,
timestamp,
sink,
queue,
}
}
pub fn default_text_formatter_config() -> TextFormatterConfig {
TextFormatterConfig::new()
}
pub fn default_sink_config() -> SinkConfig {
SinkConfig::new()
}
pub fn default_logger_config() -> LoggerConfig {
LoggerConfig::new()
}
pub(all) enum RuntimeSink {
Console(ConsoleSink)
JsonConsole(JsonConsoleSink)
TextConsole(FormattedConsoleSink)
File(FileSink)
QueuedConsole(QueuedSink[ConsoleSink])
QueuedJsonConsole(QueuedSink[JsonConsoleSink])
QueuedTextConsole(QueuedSink[FormattedConsoleSink])
QueuedFile(QueuedSink[FileSink])
}
pub struct RuntimeFileState {
file : FileSinkState
queued : Bool
pending_count : Int
dropped_count : Int
}
pub fn RuntimeFileState::new(
file : FileSinkState,
queued~ : Bool = false,
pending_count~ : Int = 0,
dropped_count~ : Int = 0,
) -> RuntimeFileState {
{ file, queued, pending_count, dropped_count }
}
fn file_sink_policy_to_json_value(policy : FileSinkPolicy) -> @json_parser.JsonValue {
let obj : Map[String, @json_parser.JsonValue] = {
"append": @json_parser.JsonValue::Bool(policy.append),
"auto_flush": @json_parser.JsonValue::Bool(policy.auto_flush),
}
match policy.rotation {
None => obj["rotation"] = @json_parser.JsonValue::Null
Some(rotation) => obj["rotation"] = file_rotation_config_to_json(rotation)
}
@json_parser.JsonValue::Object(obj)
}
pub fn file_sink_policy_to_json(policy : FileSinkPolicy) -> @json_parser.JsonValue {
file_sink_policy_to_json_value(policy)
}
pub fn stringify_file_sink_policy(policy : FileSinkPolicy, pretty~ : Bool = false) -> String {
let value = file_sink_policy_to_json_value(policy)
if pretty {
@json_parser.stringify_pretty(value, 2)
} else {
@json_parser.stringify(value)
}
}
fn file_sink_state_to_json_value(state : FileSinkState) -> @json_parser.JsonValue {
let obj : Map[String, @json_parser.JsonValue] = {
"path": @json_parser.JsonValue::String(state.path),
"available": @json_parser.JsonValue::Bool(state.available),
"append": @json_parser.JsonValue::Bool(state.append),
"auto_flush": @json_parser.JsonValue::Bool(state.auto_flush),
"open_failures": @json_parser.JsonValue::Number(state.open_failures.to_double()),
"write_failures": @json_parser.JsonValue::Number(state.write_failures.to_double()),
"flush_failures": @json_parser.JsonValue::Number(state.flush_failures.to_double()),
"rotation_failures": @json_parser.JsonValue::Number(state.rotation_failures.to_double()),
}
match state.rotation {
None => obj["rotation"] = @json_parser.JsonValue::Null
Some(rotation) => obj["rotation"] = file_rotation_config_to_json(rotation)
}
@json_parser.JsonValue::Object(obj)
}
pub fn file_sink_state_to_json(state : FileSinkState) -> @json_parser.JsonValue {
file_sink_state_to_json_value(state)
}
pub fn stringify_file_sink_state(state : FileSinkState, pretty~ : Bool = false) -> String {
let value = file_sink_state_to_json_value(state)
if pretty {
@json_parser.stringify_pretty(value, 2)
} else {
@json_parser.stringify(value)
}
}
pub fn runtime_file_state_to_json(state : RuntimeFileState) -> @json_parser.JsonValue {
@json_parser.JsonValue::Object({
"file": file_sink_state_to_json_value(state.file),
"queued": @json_parser.JsonValue::Bool(state.queued),
"pending_count": @json_parser.JsonValue::Number(state.pending_count.to_double()),
"dropped_count": @json_parser.JsonValue::Number(state.dropped_count.to_double()),
})
}
pub fn stringify_runtime_file_state(state : RuntimeFileState, pretty~ : Bool = false) -> String {
let value = runtime_file_state_to_json(state)
if pretty {
@json_parser.stringify_pretty(value, 2)
} else {
@json_parser.stringify(value)
}
}
pub impl Sink for RuntimeSink with write(self, rec) {
match self {
Console(sink) => sink.write(rec)
JsonConsole(sink) => sink.write(rec)
TextConsole(sink) => sink.write(rec)
File(sink) => sink.write(rec)
QueuedConsole(sink) => sink.write(rec)
QueuedJsonConsole(sink) => sink.write(rec)
QueuedTextConsole(sink) => sink.write(rec)
QueuedFile(sink) => sink.write(rec)
}
}
pub fn RuntimeSink::flush(self : RuntimeSink) -> Int {
match self {
Console(_) => 0
JsonConsole(_) => 0
TextConsole(_) => 0
File(sink) => if sink.flush() { 1 } else { 0 }
QueuedConsole(sink) => sink.flush()
QueuedJsonConsole(sink) => sink.flush()
QueuedTextConsole(sink) => sink.flush()
QueuedFile(sink) => sink.flush()
}
}
pub fn RuntimeSink::drain(self : RuntimeSink, max_items~ : Int = -1) -> Int {
match self {
Console(_) => 0
JsonConsole(_) => 0
TextConsole(_) => 0
File(sink) => if sink.flush() { 1 } else { 0 }
QueuedConsole(sink) => sink.drain(max_items=max_items)
QueuedJsonConsole(sink) => sink.drain(max_items=max_items)
QueuedTextConsole(sink) => sink.drain(max_items=max_items)
QueuedFile(sink) => sink.drain(max_items=max_items)
}
}
pub fn RuntimeSink::close(self : RuntimeSink) -> Bool {
match self {
Console(_) => true
JsonConsole(_) => true
TextConsole(_) => true
File(sink) => sink.close()
QueuedConsole(_) => true
QueuedJsonConsole(_) => true
QueuedTextConsole(_) => true
QueuedFile(sink) => sink.sink.close()
}
}
pub fn RuntimeSink::pending_count(self : RuntimeSink) -> Int {
match self {
Console(_) => 0
JsonConsole(_) => 0
TextConsole(_) => 0
File(_) => 0
QueuedConsole(sink) => sink.pending_count()
QueuedJsonConsole(sink) => sink.pending_count()
QueuedTextConsole(sink) => sink.pending_count()
QueuedFile(sink) => sink.pending_count()
}
}
pub fn RuntimeSink::dropped_count(self : RuntimeSink) -> Int {
match self {
Console(_) => 0
JsonConsole(_) => 0
TextConsole(_) => 0
File(_) => 0
QueuedConsole(sink) => sink.dropped_count()
QueuedJsonConsole(sink) => sink.dropped_count()
QueuedTextConsole(sink) => sink.dropped_count()
QueuedFile(sink) => sink.dropped_count()
}
}
pub fn RuntimeSink::file_available(self : RuntimeSink) -> Bool {
match self {
File(sink) => sink.is_available()
QueuedFile(sink) => sink.sink.is_available()
_ => false
}
}
pub fn RuntimeSink::file_reopen(self : RuntimeSink, append~ : Bool? = None) -> Bool {
match self {
File(sink) => sink.reopen(append=append)
QueuedFile(sink) => sink.sink.reopen(append=append)
_ => false
}
}
pub fn RuntimeSink::file_reopen_with_current_policy(self : RuntimeSink) -> Bool {
match self {
File(sink) => sink.reopen_with_current_policy()
QueuedFile(sink) => sink.sink.reopen_with_current_policy()
_ => false
}
}
pub fn RuntimeSink::file_reopen_append(self : RuntimeSink) -> Bool {
match self {
File(sink) => sink.reopen_append()
QueuedFile(sink) => sink.sink.reopen_append()
_ => false
}
}
pub fn RuntimeSink::file_reopen_truncate(self : RuntimeSink) -> Bool {
match self {
File(sink) => sink.reopen_truncate()
QueuedFile(sink) => sink.sink.reopen_truncate()
_ => false
}
}
pub fn RuntimeSink::file_append_mode(self : RuntimeSink) -> Bool {
match self {
File(sink) => sink.append_mode()
QueuedFile(sink) => sink.sink.append_mode()
_ => false
}
}
pub fn RuntimeSink::file_set_append_mode(self : RuntimeSink, append : Bool) -> Bool {
match self {
File(sink) => {
sink.set_append_mode(append)
true
}
QueuedFile(sink) => {
sink.sink.set_append_mode(append)
true
}
_ => false
}
}
pub fn RuntimeSink::file_path(self : RuntimeSink) -> String {
match self {
File(sink) => sink.path()
QueuedFile(sink) => sink.sink.path()
_ => ""
}
}
pub fn RuntimeSink::file_auto_flush(self : RuntimeSink) -> Bool {
match self {
File(sink) => sink.auto_flush_enabled()
QueuedFile(sink) => sink.sink.auto_flush_enabled()
_ => false
}
}
pub fn RuntimeSink::file_rotation_enabled(self : RuntimeSink) -> Bool {
match self {
File(sink) => sink.rotation_enabled()
QueuedFile(sink) => sink.sink.rotation_enabled()
_ => false
}
}
pub fn RuntimeSink::file_rotation_config(self : RuntimeSink) -> FileRotation? {
match self {
File(sink) => sink.rotation_config()
QueuedFile(sink) => sink.sink.rotation_config()
_ => None
}
}
pub fn RuntimeSink::file_set_auto_flush(self : RuntimeSink, enabled : Bool) -> Bool {
match self {
File(sink) => {
sink.set_auto_flush(enabled)
true
}
QueuedFile(sink) => {
sink.sink.set_auto_flush(enabled)
true
}
_ => false
}
}
pub fn RuntimeSink::file_set_policy(self : RuntimeSink, policy : FileSinkPolicy) -> Bool {
match self {
File(sink) => {
sink.set_policy(policy)
true
}
QueuedFile(sink) => {
sink.sink.set_policy(policy)
true
}
_ => false
}
}
pub fn RuntimeSink::file_set_rotation(self : RuntimeSink, rotation : FileRotation?) -> Bool {
match self {
File(sink) => {
sink.set_rotation(rotation)
true
}
QueuedFile(sink) => {
sink.sink.set_rotation(rotation)
true
}
_ => false
}
}
pub fn RuntimeSink::file_clear_rotation(self : RuntimeSink) -> Bool {
match self {
File(sink) => {
sink.clear_rotation()
true
}
QueuedFile(sink) => {
sink.sink.clear_rotation()
true
}
_ => false
}
}
pub fn RuntimeSink::file_flush(self : RuntimeSink) -> Bool {
match self {
File(sink) => sink.flush()
QueuedFile(sink) => {
ignore(sink.flush())
sink.sink.flush()
}
_ => false
}
}
pub fn RuntimeSink::file_close(self : RuntimeSink) -> Bool {
match self {
File(sink) => sink.close()
QueuedFile(sink) => {
ignore(sink.flush())
sink.sink.close()
}
_ => false
}
}
pub fn RuntimeSink::file_open_failures(self : RuntimeSink) -> Int {
match self {
File(sink) => sink.open_failures()
QueuedFile(sink) => sink.sink.open_failures()
_ => 0
}
}
pub fn RuntimeSink::file_write_failures(self : RuntimeSink) -> Int {
match self {
File(sink) => sink.write_failures()
QueuedFile(sink) => sink.sink.write_failures()
_ => 0
}
}
pub fn RuntimeSink::file_flush_failures(self : RuntimeSink) -> Int {
match self {
File(sink) => sink.flush_failures()
QueuedFile(sink) => sink.sink.flush_failures()
_ => 0
}
}
pub fn RuntimeSink::file_rotation_failures(self : RuntimeSink) -> Int {
match self {
File(sink) => sink.rotation_failures()
QueuedFile(sink) => sink.sink.rotation_failures()
_ => 0
}
}
pub fn RuntimeSink::file_reset_failure_counters(self : RuntimeSink) -> Bool {
match self {
File(sink) => {
sink.reset_failure_counters()
true
}
QueuedFile(sink) => {
sink.sink.reset_failure_counters()
true
}
_ => false
}
}
pub fn RuntimeSink::file_reset_policy(self : RuntimeSink) -> Bool {
match self {
File(sink) => {
sink.reset_policy()
true
}
QueuedFile(sink) => {
sink.sink.reset_policy()
true
}
_ => false
}
}
pub fn RuntimeSink::file_policy(self : RuntimeSink) -> FileSinkPolicy {
match self {
File(sink) => sink.policy()
QueuedFile(sink) => sink.sink.policy()
_ => FileSinkPolicy::new(append=false, auto_flush=false, rotation=None)
}
}
pub fn RuntimeSink::file_default_policy(self : RuntimeSink) -> FileSinkPolicy {
match self {
File(sink) => sink.default_policy()
QueuedFile(sink) => sink.sink.default_policy()
_ => FileSinkPolicy::new(append=false, auto_flush=false, rotation=None)
}
}
pub fn RuntimeSink::file_policy_matches_default(self : RuntimeSink) -> Bool {
match self {
File(sink) => sink.policy_matches_default()
QueuedFile(sink) => sink.sink.policy_matches_default()
_ => false
}
}
pub fn RuntimeSink::file_state(self : RuntimeSink) -> FileSinkState {
match self {
File(sink) => sink.state()
QueuedFile(sink) => sink.sink.state()
_ => FileSinkState::new(
"",
available=false,
append=false,
auto_flush=false,
rotation=None,
open_failures=0,
write_failures=0,
flush_failures=0,
rotation_failures=0,
)
}
}
pub fn RuntimeSink::file_runtime_state(self : RuntimeSink) -> RuntimeFileState? {
match self {
File(sink) => Some(RuntimeFileState::new(sink.state()))
QueuedFile(sink) => Some(
RuntimeFileState::new(
sink.sink.state(),
queued=true,
pending_count=sink.pending_count(),
dropped_count=sink.dropped_count(),
),
)
_ => None
}
}
pub type ConfiguredLogger = Logger[RuntimeSink]
pub fn ConfiguredLogger::flush(self : ConfiguredLogger) -> Int {
self.sink.flush()
}
pub fn ConfiguredLogger::drain(self : ConfiguredLogger, max_items~ : Int = -1) -> Int {
self.sink.drain(max_items=max_items)
}
pub fn ConfiguredLogger::close(self : ConfiguredLogger) -> Bool {
self.sink.close()
}
pub fn ConfiguredLogger::pending_count(self : ConfiguredLogger) -> Int {
self.sink.pending_count()
}
pub fn ConfiguredLogger::dropped_count(self : ConfiguredLogger) -> Int {
self.sink.dropped_count()
}
pub fn ConfiguredLogger::file_available(self : ConfiguredLogger) -> Bool {
self.sink.file_available()
}
pub fn ConfiguredLogger::file_reopen(self : ConfiguredLogger, append~ : Bool? = None) -> Bool {
self.sink.file_reopen(append=append)
}
pub fn ConfiguredLogger::file_reopen_with_current_policy(self : ConfiguredLogger) -> Bool {
self.sink.file_reopen_with_current_policy()
}
pub fn ConfiguredLogger::file_reopen_append(self : ConfiguredLogger) -> Bool {
self.sink.file_reopen_append()
}
pub fn ConfiguredLogger::file_reopen_truncate(self : ConfiguredLogger) -> Bool {
self.sink.file_reopen_truncate()
}
pub fn ConfiguredLogger::file_append_mode(self : ConfiguredLogger) -> Bool {
self.sink.file_append_mode()
}
pub fn ConfiguredLogger::file_set_append_mode(self : ConfiguredLogger, append : Bool) -> Bool {
self.sink.file_set_append_mode(append)
}
pub fn ConfiguredLogger::file_path(self : ConfiguredLogger) -> String {
self.sink.file_path()
}
pub fn ConfiguredLogger::file_auto_flush(self : ConfiguredLogger) -> Bool {
self.sink.file_auto_flush()
}
pub fn ConfiguredLogger::file_rotation_enabled(self : ConfiguredLogger) -> Bool {
self.sink.file_rotation_enabled()
}
pub fn ConfiguredLogger::file_rotation_config(self : ConfiguredLogger) -> FileRotation? {
self.sink.file_rotation_config()
}
pub fn ConfiguredLogger::file_set_auto_flush(self : ConfiguredLogger, enabled : Bool) -> Bool {
self.sink.file_set_auto_flush(enabled)
}
pub fn ConfiguredLogger::file_set_policy(self : ConfiguredLogger, policy : FileSinkPolicy) -> Bool {
self.sink.file_set_policy(policy)
}
pub fn ConfiguredLogger::file_set_rotation(
self : ConfiguredLogger,
rotation : FileRotation?,
) -> Bool {
self.sink.file_set_rotation(rotation)
}
pub fn ConfiguredLogger::file_clear_rotation(self : ConfiguredLogger) -> Bool {
self.sink.file_clear_rotation()
}
pub fn ConfiguredLogger::file_flush(self : ConfiguredLogger) -> Bool {
self.sink.file_flush()
}
pub fn ConfiguredLogger::file_close(self : ConfiguredLogger) -> Bool {
self.sink.file_close()
}
pub fn ConfiguredLogger::file_open_failures(self : ConfiguredLogger) -> Int {
self.sink.file_open_failures()
}
pub fn ConfiguredLogger::file_write_failures(self : ConfiguredLogger) -> Int {
self.sink.file_write_failures()
}
pub fn ConfiguredLogger::file_flush_failures(self : ConfiguredLogger) -> Int {
self.sink.file_flush_failures()
}
pub fn ConfiguredLogger::file_rotation_failures(self : ConfiguredLogger) -> Int {
self.sink.file_rotation_failures()
}
pub fn ConfiguredLogger::file_reset_failure_counters(self : ConfiguredLogger) -> Bool {
self.sink.file_reset_failure_counters()
}
pub fn ConfiguredLogger::file_reset_policy(self : ConfiguredLogger) -> Bool {
self.sink.file_reset_policy()
}
pub fn ConfiguredLogger::file_policy(self : ConfiguredLogger) -> FileSinkPolicy {
self.sink.file_policy()
}
pub fn ConfiguredLogger::file_default_policy(self : ConfiguredLogger) -> FileSinkPolicy {
self.sink.file_default_policy()
}
pub fn ConfiguredLogger::file_policy_matches_default(self : ConfiguredLogger) -> Bool {
self.sink.file_policy_matches_default()
}
pub fn ConfiguredLogger::file_state(self : ConfiguredLogger) -> FileSinkState {
self.sink.file_state()
}
pub fn ConfiguredLogger::file_runtime_state(self : ConfiguredLogger) -> RuntimeFileState? {
self.sink.file_runtime_state()
}
fn expect_object(
value : @json_parser.JsonValue,
context : String,
) -> Map[String, @json_parser.JsonValue] raise ConfigError {
match value.as_object() {
Some(obj) => obj
None => raise ConfigError::InvalidConfig("Expected object at " + context)
}
}
fn get_string(
obj : Map[String, @json_parser.JsonValue],
key : String,
default~ : String = "",
) -> String raise ConfigError {
match obj.get(key) {
None => default
Some(value) => match value.as_string() {
Some(text) => text
None => raise ConfigError::InvalidConfig("Expected string at key " + key)
}
}
}
fn get_optional_string(
obj : Map[String, @json_parser.JsonValue],
key : String,
) -> String? raise ConfigError {
match obj.get(key) {
None => None
Some(value) => match value.as_string() {
Some(text) => Some(text)
None => raise ConfigError::InvalidConfig("Expected string at key " + key)
}
}
}
fn get_bool(
obj : Map[String, @json_parser.JsonValue],
key : String,
default~ : Bool,
) -> Bool raise ConfigError {
match obj.get(key) {
None => default
Some(value) => match value.as_bool() {
Some(flag) => flag
None => raise ConfigError::InvalidConfig("Expected bool at key " + key)
}
}
}
fn get_int(
obj : Map[String, @json_parser.JsonValue],
key : String,
default~ : Int,
) -> Int raise ConfigError {
match obj.get(key) {
None => default
Some(value) => match value.as_number() {
Some(number) => number.to_int()
None => raise ConfigError::InvalidConfig("Expected number at key " + key)
}
}
}
fn parse_level(name : String) -> Level raise ConfigError {
match name.to_upper() {
"TRACE" => Level::Trace
"DEBUG" => Level::Debug
"INFO" => Level::Info
"WARN" => Level::Warn
"ERROR" => Level::Error
_ => raise ConfigError::InvalidConfig("Unsupported level: " + name)
}
}
fn parse_overflow(name : String) -> QueueOverflowPolicy raise ConfigError {
match name.to_upper() {
"DROPNEWEST" => QueueOverflowPolicy::DropNewest
"DROPPOLDEST" => QueueOverflowPolicy::DropOldest
"DROPOLDEST" => QueueOverflowPolicy::DropOldest
_ => raise ConfigError::InvalidConfig("Unsupported queue overflow policy: " + name)
}
}
fn parse_sink_kind(name : String) -> SinkKind raise ConfigError {
match name.to_upper() {
"CONSOLE" => SinkKind::Console
"JSON_CONSOLE" => SinkKind::JsonConsole
"JSONCONSOLE" => SinkKind::JsonConsole
"TEXT_CONSOLE" => SinkKind::TextConsole
"TEXTCONSOLE" => SinkKind::TextConsole
"FILE" => SinkKind::File
_ => raise ConfigError::InvalidConfig("Unsupported sink kind: " + name)
}
}
fn parse_color_mode(name : String) -> ColorMode raise ConfigError {
match name.to_upper() {
"NEVER" => ColorMode::Never
"AUTO" => ColorMode::Auto
"ALWAYS" => ColorMode::Always
_ => raise ConfigError::InvalidConfig("Unsupported color mode: " + name)
}
}
fn parse_style_markup_mode(name : String) -> StyleMarkupMode raise ConfigError {
match name.to_upper() {
"DISABLED" => StyleMarkupMode::Disabled
"BUILTIN" => StyleMarkupMode::Builtin
"FULL" => StyleMarkupMode::Full
_ => raise ConfigError::InvalidConfig("Unsupported style markup mode: " + name)
}
}
fn sink_kind_label(kind : SinkKind) -> String {
match kind {
SinkKind::Console => "console"
SinkKind::JsonConsole => "json_console"
SinkKind::TextConsole => "text_console"
SinkKind::File => "file"
}
}
fn parse_text_formatter_config(value : @json_parser.JsonValue) -> TextFormatterConfig raise ConfigError {
let obj = expect_object(value, "text_formatter")
TextFormatterConfig::new(
show_timestamp=get_bool(obj, "show_timestamp", default=true),
show_level=get_bool(obj, "show_level", default=true),
show_target=get_bool(obj, "show_target", default=true),
show_fields=get_bool(obj, "show_fields", default=true),
separator=get_string(obj, "separator", default=" "),
field_separator=get_string(obj, "field_separator", default=" "),
template=get_string(obj, "template", default=""),
color_mode=parse_color_mode(get_string(obj, "color_mode", default="never")),
style_markup=parse_style_markup_mode(get_string(obj, "style_markup", default="full")),
style_tags=match obj.get("style_tags") {
None => {}
Some(inner) => parse_style_tags_config(inner)
},
)
}
fn parse_text_style_config(
value : @json_parser.JsonValue,
context : String,
) -> TextStyle raise ConfigError {
let obj = expect_object(value, context)
text_style(
fg=get_optional_string(obj, "fg"),
bg=get_optional_string(obj, "bg"),
bold=get_bool(obj, "bold", default=false),
dim=get_bool(obj, "dim", default=false),
italic=get_bool(obj, "italic", default=false),
underline=get_bool(obj, "underline", default=false),
)
}
fn parse_style_tags_config(value : @json_parser.JsonValue) -> Map[String, TextStyle] raise ConfigError {
let obj = expect_object(value, "text_formatter.style_tags")
let style_tags : Map[String, TextStyle] = {}
for name, item in obj {
style_tags[name] = parse_text_style_config(item, "text_formatter.style_tags." + name)
}
style_tags
}
fn parse_queue_config(value : @json_parser.JsonValue) -> QueueConfig raise ConfigError {
let obj = expect_object(value, "queue")
QueueConfig::new(
get_int(obj, "max_pending", default=0),
overflow=parse_overflow(get_string(obj, "overflow", default="DropNewest")),
)
}
fn parse_file_rotation_config(value : @json_parser.JsonValue) -> FileRotation raise ConfigError {
let obj = expect_object(value, "sink.rotation")
file_rotation(
get_int(obj, "max_bytes", default=1),
max_backups=get_int(obj, "max_backups", default=1),
)
}
fn parse_sink_config(value : @json_parser.JsonValue) -> SinkConfig raise ConfigError {
let obj = expect_object(value, "sink")
let kind = parse_sink_kind(get_string(obj, "kind", default="console"))
let formatter = match obj.get("text_formatter") {
None => default_text_formatter_config()
Some(inner) => parse_text_formatter_config(inner)
}
let path = get_string(obj, "path", default="")
match kind {
SinkKind::File => if path == "" {
raise ConfigError::InvalidConfig("File sink requires non-empty path")
}
_ => ()
}
SinkConfig::new(
kind=kind,
path=path,
append=get_bool(obj, "append", default=true),
auto_flush=get_bool(obj, "auto_flush", default=true),
rotation=match obj.get("rotation") {
None => None
Some(inner) => Some(parse_file_rotation_config(inner))
},
text_formatter=formatter,
)
}
pub fn parse_logger_config_text(input : String) -> LoggerConfig raise ConfigError {
let root = @json_parser.parse(input) catch {
e => raise ConfigError::InvalidConfig("Invalid JSON: " + e.to_string())
}
let obj = expect_object(root, "root")
LoggerConfig::new(
min_level=parse_level(get_string(obj, "min_level", default="INFO")),
target=get_string(obj, "target", default=""),
timestamp=get_bool(obj, "timestamp", default=false),
sink=match obj.get("sink") {
None => default_sink_config()
Some(value) => parse_sink_config(value)
},
queue=match obj.get("queue") {
None => None
Some(value) => Some(parse_queue_config(value))
},
)
}
pub fn queue_config_to_json(queue : QueueConfig) -> @json_parser.JsonValue {
@json_parser.JsonValue::Object({
"max_pending": @json_parser.JsonValue::Number(queue.max_pending.to_double()),
"overflow": @json_parser.JsonValue::String(match queue.overflow {
QueueOverflowPolicy::DropNewest => "DropNewest"
QueueOverflowPolicy::DropOldest => "DropOldest"
}),
})
}
pub fn stringify_queue_config(queue : QueueConfig, pretty~ : Bool = false) -> String {
let value = queue_config_to_json(queue)
if pretty {
@json_parser.stringify_pretty(value, 2)
} else {
@json_parser.stringify(value)
}
}
pub fn text_formatter_config_to_json(config : TextFormatterConfig) -> @json_parser.JsonValue {
let obj : Map[String, @json_parser.JsonValue] = {
"show_timestamp": @json_parser.JsonValue::Bool(config.show_timestamp),
"show_level": @json_parser.JsonValue::Bool(config.show_level),
"show_target": @json_parser.JsonValue::Bool(config.show_target),
"show_fields": @json_parser.JsonValue::Bool(config.show_fields),
"separator": @json_parser.JsonValue::String(config.separator),
"field_separator": @json_parser.JsonValue::String(config.field_separator),
"template": @json_parser.JsonValue::String(config.template),
"color_mode": @json_parser.JsonValue::String(color_mode_label(config.color_mode)),
"style_markup": @json_parser.JsonValue::String(style_markup_mode_label(config.style_markup)),
}
if config.style_tags.length() != 0 {
obj["style_tags"] = style_tags_config_to_json(config.style_tags)
}
@json_parser.JsonValue::Object(obj)
}
fn text_style_config_to_json(style : TextStyle) -> @json_parser.JsonValue {
let obj : Map[String, @json_parser.JsonValue] = {
"bold": @json_parser.JsonValue::Bool(style.bold),
"dim": @json_parser.JsonValue::Bool(style.dim),
"italic": @json_parser.JsonValue::Bool(style.italic),
"underline": @json_parser.JsonValue::Bool(style.underline),
}
match style.fg {
Some(value) => obj["fg"] = @json_parser.JsonValue::String(value)
None => ()
}
match style.bg {
Some(value) => obj["bg"] = @json_parser.JsonValue::String(value)
None => ()
}
@json_parser.JsonValue::Object(obj)
}
fn style_tags_config_to_json(style_tags : Map[String, TextStyle]) -> @json_parser.JsonValue {
let obj : Map[String, @json_parser.JsonValue] = {}
for name, style in style_tags {
obj[name] = text_style_config_to_json(style)
}
@json_parser.JsonValue::Object(obj)
}
pub fn stringify_text_formatter_config(
config : TextFormatterConfig,
pretty~ : Bool = false,
) -> String {
let value = text_formatter_config_to_json(config)
if pretty {
@json_parser.stringify_pretty(value, 2)
} else {
@json_parser.stringify(value)
}
}
fn file_rotation_config_to_json(config : FileRotation) -> @json_parser.JsonValue {
@json_parser.JsonValue::Object({
"max_bytes": @json_parser.JsonValue::Number(config.max_bytes.to_double()),
"max_backups": @json_parser.JsonValue::Number(config.max_backups.to_double()),
})
}
pub fn sink_config_to_json(config : SinkConfig) -> @json_parser.JsonValue {
let obj : Map[String, @json_parser.JsonValue] = {
"kind": @json_parser.JsonValue::String(sink_kind_label(config.kind)),
"path": @json_parser.JsonValue::String(config.path),
"append": @json_parser.JsonValue::Bool(config.append),
"auto_flush": @json_parser.JsonValue::Bool(config.auto_flush),
"text_formatter": text_formatter_config_to_json(config.text_formatter),
}
match config.rotation {
None => ()
Some(rotation) => obj["rotation"] = file_rotation_config_to_json(rotation)
}
@json_parser.JsonValue::Object(obj)
}
pub fn stringify_sink_config(config : SinkConfig, pretty~ : Bool = false) -> String {
let value = sink_config_to_json(config)
if pretty {
@json_parser.stringify_pretty(value, 2)
} else {
@json_parser.stringify(value)
}
}
pub fn logger_config_to_json(config : LoggerConfig) -> @json_parser.JsonValue {
let obj : Map[String, @json_parser.JsonValue] = {
"min_level": @json_parser.JsonValue::String(config.min_level.label()),
"target": @json_parser.JsonValue::String(config.target),
"timestamp": @json_parser.JsonValue::Bool(config.timestamp),
"sink": sink_config_to_json(config.sink),
}
match config.queue {
None => ()
Some(queue) => obj["queue"] = queue_config_to_json(queue)
}
@json_parser.JsonValue::Object(obj)
}
pub fn stringify_logger_config(config : LoggerConfig, pretty~ : Bool = false) -> String {
let value = logger_config_to_json(config)
if pretty {
@json_parser.stringify_pretty(value, 2)
} else {
@json_parser.stringify(value)
}
}
fn build_runtime_sink(config : SinkConfig) -> RuntimeSink {
match config.kind {
SinkKind::Console => RuntimeSink::Console(console_sink())
SinkKind::JsonConsole => RuntimeSink::JsonConsole(json_console_sink())
SinkKind::TextConsole => RuntimeSink::TextConsole(
text_console_sink(config.text_formatter.to_formatter()),
)
SinkKind::File => RuntimeSink::File(
file_sink(
config.path,
append=config.append,
auto_flush=config.auto_flush,
rotation=config.rotation,
formatter=fn(rec) {
format_text(rec, formatter=config.text_formatter.to_formatter())
},
),
)
}
}
fn apply_queue_config(sink : RuntimeSink, queue : QueueConfig) -> RuntimeSink {
match sink {
Console(inner) => RuntimeSink::QueuedConsole(
queued_sink(inner, max_pending=queue.max_pending, overflow=queue.overflow),
)
JsonConsole(inner) => RuntimeSink::QueuedJsonConsole(
queued_sink(inner, max_pending=queue.max_pending, overflow=queue.overflow),
)
TextConsole(inner) => RuntimeSink::QueuedTextConsole(
queued_sink(inner, max_pending=queue.max_pending, overflow=queue.overflow),
)
File(inner) => RuntimeSink::QueuedFile(
queued_sink(inner, max_pending=queue.max_pending, overflow=queue.overflow),
)
QueuedConsole(_) => sink
QueuedJsonConsole(_) => sink
QueuedTextConsole(_) => sink
QueuedFile(_) => sink
}
}
pub fn build_logger(config : LoggerConfig) -> ConfiguredLogger {
let sink = build_runtime_sink(config.sink)
let actual_sink = match config.queue {
None => sink
Some(queue) => apply_queue_config(sink, queue)
}
Logger::new(actual_sink, min_level=config.min_level, target=config.target)
.with_timestamp(enabled=config.timestamp)
}
pub fn parse_and_build_logger(input : String) -> ConfiguredLogger raise ConfigError {
build_logger(parse_logger_config_text(input))
}