Documentation
¶
Overview ¶
Package sllm is the 3rd iteration of the reference implementation for the Structured Logging Lightweight Markup format.
The goal is to create a human-readable format for the message part of log entries that also makes the parameters in the log message reliably machine-readable. This is a task generally performed by markup languages. However, sllm is intended to be much less intrusive than, for example, XML or JSON. The traditional log message:
2019/01/11 19:32:44 added 7 ⨉ Hat to shopping cart by John Doe
would become something like (depending on the choice of parameter names)
2019/01/11 19:32:44 added `count:7` ⨉ `item:Hat` to shopping cart by `user:John Doe`
This is still readable by humans but can also be parsed reliably by machines. Machine reading would not fail even if the message template changes the order of the parameters. Careful choice of parameter names can make the messages even more meaningful.
This package is no logging library—it provides functions to create and parse sllm messages.
Example ¶
// Positional arguments FprintIdx(os.Stdout, "added `count` ⨉ `item` to shopping cart by `user`\n", 7, "Hat", "John Doe", ) // Named arguments Fprint(os.Stdout, "added `count` ⨉ `item` to shopping cart by `user`\n", NmArgs(map[string]any{ "count": 7, "item": "Hat", "user": "John Doe", }), )
Output: added `count:7` ⨉ `item:Hat` to shopping cart by `user:John Doe` added `count:7` ⨉ `item:Hat` to shopping cart by `user:John Doe`
Index ¶
- Constants
- func Append(to []byte, tmpl string, args ArgsFunc) ([]byte, error)
- func AppendArg(to []byte, v any) []byte
- func Error(tmpl string, args ArgsFunc) error
- func ErrorIdx(tmpl string, args ...any) error
- func EscBytes(to, val []byte) []byte
- func EscString(to []byte, val string) []byte
- func Fprint(w io.Writer, tmpl string, args ArgsFunc) (int, error)
- func FprintIdx(w io.Writer, tmpl string, args ...any) (int, error)
- func IdxArgs(args ...any) func([]byte, int, string) ([]byte, error)
- func IdxArgsDefault(d any, args ...any) func([]byte, int, string) ([]byte, error)
- func NmArgs(args map[string]any) func([]byte, int, string) ([]byte, error)
- func NmArgsDefault(d any, args map[string]any) func([]byte, int, string) ([]byte, error)
- func Parameters(tmpl string, a []string) ([]string, error)
- func Parse(msg string, tmpl *bytes.Buffer, ...) error
- func ParseMap(msg string, tmpl *bytes.Buffer) (map[string][]any, error)
- func String(tmpl string, args ArgsFunc) (string, error)
- func StringIdx(tmpl string, args ...any) (string, error)
- type Appender
- type ArgError
- type ArgErrors
- type ArgsFunc
- type TimeFormat
Examples ¶
Constants ¶
const TDefault = TimeFormat(0)
Variables ¶
This section is empty.
Functions ¶
func Append ¶
Example (IdxArgs) ¶
args := IdxArgsDefault("???", testSvc, testSig, testProc, testName, testNow) var buf []byte buf, _ = Append(buf, testTmpl, args) os.Stdout.Write(buf) fmt.Println()
Output: `service:rsyslog`: Sent `signal:SIGHUP` to main `process:1611` (`name:rsyslogd`) on client request `at:11-27 Mo 21:30:00+00`.
Example (NmArgs) ¶
args := NmArgs(map[string]any{ "service": testSvc, "signal": testSig, "process": testProc, "name": testName, "at": testNow, }) var buf []byte buf, _ = Append(buf, testTmpl, args) os.Stdout.Write(buf) fmt.Println()
Output: `service:rsyslog`: Sent `signal:SIGHUP` to main `process:1611` (`name:rsyslogd`) on client request `at:11-27 Mo 21:30:00+00`.
Example (TestArgsN) ¶
var buf []byte buf, _ = Append(buf, testTmpl, testArgsN) os.Stdout.Write(buf) fmt.Println()
Output: `service:rsyslog`: Sent `signal:SIGHUP` to main `process:1611` (`name:rsyslogd`) on client request `at:11-27 Mo 21:30:00+00`.
Example (Time) ¶
t := time.Date(2023, 11, 27, 21, 30, 00, 0, time.UTC) buf, _ := Append(nil, "`its`", IdxArgs(t)) os.Stdout.Write(buf) fmt.Println()
Output: `its:2023-11-27 21:30:00 +0000 UTC`
func IdxArgsDefault ¶
func NmArgsDefault ¶
func Parameters ¶
Parameters extracs the parameter names from template tmpl and appends them to a.
func Parse ¶
func Parse(msg string, tmpl *bytes.Buffer, onArg func(name, value string, argError bool) error) error
Parse parses a sllm message create by Append and calls onArg for every `name:value` parameter it finds in the message. When a non-nil buffer is passed as tmpl Parse will also reconstruct the original template into the buffer. Note that the template is appended to tmpl's content.
func ParseMap ¶
ParseMap uses Parse to create a map with all parameters assigned to an argument in the passed message msg. ParseMap can also reconstruct the template when passing a Buffer to tmpl.
Example ¶
var tmpl bytes.Buffer args, _ := ParseMap( "added `count:7` ⨉ `item:Hat` to shopping cart by `user:John Doe`", &tmpl, ) fmt.Println(tmpl.String()) for k, v := range args { fmt.Printf("%s:[%s]\n", k, v) }
Output: added `count` ⨉ `item` to shopping cart by `user` count:[[7]] item:[[Hat]] user:[[John Doe]]
Types ¶
type ArgsFunc ¶
ArgsFunc appends the escaped argument i with name n to the buffer buff and returns the resulting buffer. Even in case of error, buf must be returned. To escape an argument implementers should use EscString or EscBytes.
type TimeFormat ¶
type TimeFormat int32
Example ¶
tz := time.FixedZone("West", -int((3 * time.Hour).Seconds())) t := time.Date(2023, 05, 04, 21, 43, 1, 2003000, tz) os.Stdout.Write(TimeFormat(0).Fmt(t).AppendSllm(nil)) fmt.Print("\n\n") os.Stdout.Write(TUTC.Fmt(t).AppendSllm(nil)) fmt.Println() os.Stdout.Write(TNoDate.Fmt(t).AppendSllm(nil)) fmt.Println() os.Stdout.Write(TNoWeekday.Fmt(t).AppendSllm(nil)) fmt.Println() os.Stdout.Write(TYear.Fmt(t).AppendSllm(nil)) fmt.Println() os.Stdout.Write(TNoClock.Fmt(t).AppendSllm(nil)) fmt.Println() os.Stdout.Write(TMillis.Fmt(t).AppendSllm(nil)) fmt.Println() os.Stdout.Write(TMicros.Fmt(t).AppendSllm(nil)) fmt.Println()
Output: 05-04 Th 21:43:01-03 05-05 Fr 00:43:01 21:43:01-03 05-04 21:43:01-03 2023-05-04 Th 21:43:01-03 05-04 Th -03 05-04 Th 21:43:01.002-03 05-04 Th 21:43:01.002003-03
const ( TUTC TimeFormat = 1 << iota TNoDate TNoWeekday TYear TNoClock TMillis TMicros )
func (TimeFormat) Fmt ¶
func (tf TimeFormat) Fmt(t time.Time) timeFormatter