Skip to content

Commit

Permalink
feat: detect json and logfmt log types
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhackl committed Oct 31, 2024
1 parent 8996dd5 commit 765d23c
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion modules/utils/logs/log-levels.alloy
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ declare "default_level" {
}
}

// default level to unknown
// default type to unknown
stage.static_labels {
values = {
log_type = "unknown",
Expand Down Expand Up @@ -112,6 +112,59 @@ declare "default_level" {
}
}

// check to see if the log line matches the json format
stage.match {
// unescaped regex: ^\s*\{.+\}\s*$
selector = "{level=\"" + argument.default_level.value + "\"} |~ \"^\\\\s*\\\\{.+\\\\}\\\\s*$\""

// set the log_type
stage.static_labels{
values = {
log_type = "json",
}
}

// extract the level
stage.json {
expressions = {
level = "level || lvl || loglevel || LogLevel || log_level || logLevel || log_lvl || logLvl || levelname || levelName || LevelName",
}
}

// set the extracted level as a label
stage.labels {
values = {
level = "",
}
}
}

// check to see if the log line matches the logfmt format
stage.match {
// unescaped regex: ^(\w+=("[^"]*"|\S+))(\s+(\w+=("[^"]*"|\S+)))*\s*
selector = "{level=\"" + argument.default_level.value + "\"} |~ \"^(\\\\w+=(\\\"[^\\\"]*\\\"|\\\\S+))(\\\\s+(\\\\w+=(\\\"[^\\\"]*\\\"|\\\\S+)))*\\\\s*\""

// set the log_type
stage.static_labels{
values = {
log_type = "logfmt",
}
}

// while the level could be extracted as logfmt, this allows for multiple possible log levels formats
// i.e. loglevel=info, level=info, lvl=info, loglvl=info
stage.regex {
expression = "(log)?(level|lvl)=\"?(?P<level>\\S+)\"?"
}

// set the extracted level value as a label
stage.labels {
values = {
level = "",
}
}
}

// if the level is still unknown, do one last attempt at detecting it based on common levels
stage.match {
selector = "{level=\"" + argument.default_level.value + "\"}"
Expand Down

0 comments on commit 765d23c

Please sign in to comment.