
Filter.pudu
Pudu69 lines2.5 KB
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) -> Bool161718export fn byExcluding(predicate: Predicate) -> Filter { fn(event: &Log.Event) -> Bool { !predicate(event) } }192021export fn byIncludingOnly(predicate: Predicate) -> Filter { predicate }222324export 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}323334export fn withProperty(name: Str) -> Predicate { fn(event: &Log.Event) -> Bool { Properties.has(&event.properties, name) } }353637export fn withPropertyValue(name: Str, expected: Log.Value) -> Predicate {38 fn(event: &Log.Event) -> Bool { Properties.find(&event.properties, name) == Some(expected) }39}404142export 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}505152export fn atLeast(minimum: Log.Level) -> Predicate { fn(event: &Log.Event) -> Bool { Levels.passes(event.level, minimum) } }535455export fn withFailure() -> Predicate { fn(event: &Log.Event) -> Bool { event.failure != None } }565758export fn allOf(predicates: Array[Predicate]) -> Predicate {59 fn(event: &Log.Event) -> Bool { List.all(&predicates, |predicate: Predicate| predicate(event)) }60}616263export fn anyOf(predicates: Array[Predicate]) -> Predicate {64 fn(event: &Log.Event) -> Bool { List.any(&predicates, |predicate: Predicate| predicate(event)) }65}666768export fn not(predicate: Predicate) -> Predicate { fn(event: &Log.Event) -> Bool { !predicate(event) } }69