Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-log

Structured event logging for Pudu: message templates, enrichment, filtering, formatting, and sinks

0.1.0Apache-2.01

InstallClose

Filter.pudu

Pudu69 lines2.5 KB

GitHub ↗
1/** @Log.Filter.Stage — decides which events continue to sinks */2module PuduLangLog.Filter34import Std.List as List5import PuduLangLog.Constants.Names as Names6import PuduLangLog.Domain.Levels as Levels7import PuduLangLog.Domain.Properties as Properties8import PuduLangLog.Domain.Sources as Sources9import PuduLangLog as Log1011/** @Log.Filter.Predicate — a yes or no about one event */12export type Predicate = fn(&Log.Event) -> Bool1314/** @Log.Filter.Keep — whether an event continues */15export type Filter = fn(&Log.Event) -> Bool1617/// Keeps every event the predicate rejects.18export fn byExcluding(predicate: Predicate) -> Filter { fn(event: &Log.Event) -> Bool { !predicate(event) } }1920/// Keeps only events the predicate accepts.21export fn byIncludingOnly(predicate: Predicate) -> Filter { predicate }2223/// Events whose source context is the source or lies beneath it.24export fn fromSource(source: Str) -> Predicate {25  fn(event: &Log.Event) -> Bool {26    match Properties.find(&event.properties, Names.SOURCE_CONTEXT) {27      case Some(Log.Scalar(Log.Text(context))) => Sources.beneath(source, context)28      case _ => false29    }30  }31}3233/// Events that carry a property.34export fn withProperty(name: Str) -> Predicate { fn(event: &Log.Event) -> Bool { Properties.has(&event.properties, name) } }3536/// Events whose property equals a value.37export fn withPropertyValue(name: Str, expected: Log.Value) -> Predicate {38  fn(event: &Log.Event) -> Bool { Properties.find(&event.properties, name) == Some(expected) }39}4041/// Events whose property satisfies a test.42export fn withPropertyWhere(name: Str, test: fn(&Log.Value) -> Bool) -> Predicate {43  fn(event: &Log.Event) -> Bool {44    match Properties.find(&event.properties, name) {45      case Some(found) => test(&found)46      case None => false47    }48  }49}5051/// Events at or above a level.52export fn atLeast(minimum: Log.Level) -> Predicate { fn(event: &Log.Event) -> Bool { Levels.passes(event.level, minimum) } }5354/// Events that carry a failure.55export fn withFailure() -> Predicate { fn(event: &Log.Event) -> Bool { event.failure != None } }5657/// Events every predicate accepts.58export fn allOf(predicates: Array[Predicate]) -> Predicate {59  fn(event: &Log.Event) -> Bool { List.all(&predicates, |predicate: Predicate| predicate(event)) }60}6162/// Events some predicate accepts.63export fn anyOf(predicates: Array[Predicate]) -> Predicate {64  fn(event: &Log.Event) -> Bool { List.any(&predicates, |predicate: Predicate| predicate(event)) }65}6667/// Events the predicate rejects.68export fn not(predicate: Predicate) -> Predicate { fn(event: &Log.Event) -> Bool { !predicate(event) } }69