Documentation
¶
Overview ¶
Package sllm is the reference implementation for the sllm log message format.
sllm is short for Structured Logging Lightweight Markup. Its goal is to provide a human readable format for the message part of log-entries that allows parameters in the log message to be reliably recognized by programs. A task generally addressed by markup languages. For sllm we want something much less obstrusive than e.g. 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`
Still human readable but also easy to be read by machines. Also machine reading would not break even when the message template changes the order of parameters. Careful choice of parameter names can make messages even more expressive.
This package is no logging library—it provides functions to create and parse sllm messages.
Example (ValEsc_Write) ¶
ew := valEsc{wr: os.Stdout} ew.Write([]byte("foo")) fmt.Fprintln(os.Stdout) ew.Write([]byte("`bar`")) fmt.Fprintln(os.Stdout) ew.Write([]byte("b`az")) fmt.Fprintln(os.Stdout)
Output: foo ``bar`` b``az
Index ¶
- func Error(tmpl string, a ...interface{}) error
- func Expand(wr io.Writer, tmpl string, writeArg ParamWriter) (n int, err error)
- func Expands(tmpl string, writeArg ParamWriter) (string, error)
- func ExtractParams(appendTo []string, tmpl string) ([]string, error)
- func Parse(msg string, tmpl *bytes.Buffer, onArg func(name, value string) error) error
- func ParseMap(msg string, tmpl *bytes.Buffer) (map[string][]string, error)
- type ArgMap
- type IllegalArgIndex
- type ParamWriter
- type SyntaxError
- type UndefinedArg
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Error ¶ added in v0.12.0
Example ¶
fmt.Println(Error("this is just `an` message with missing `param`", "error")) fmt.Println(Error("this will `fail", "dummy"))
Output: this is just `an:error` message with missing `param:<nil>` [sllm error:syntax error in `tmpl:this will ``fail`:`pos:11`:`desc:unterminated argument`]
func Expand ¶
Expand writes a message to the io.Writer wr by expanding all arguments of the given template tmpl. The actual process of expanding an argument is left to the given ParamWriter writeArg.
Example ¶
var writeTestArg = func(wr io.Writer, idx int, name string) (int, error) { return fmt.Fprintf(wr, "#%d/'%s'", idx, name) } Expand(os.Stdout, "want `arg1` here and `arg2` here", writeTestArg) fmt.Println() Expand(os.Stdout, "template with backtick '``' and an `arg` here", writeTestArg) fmt.Println() Expand(os.Stdout, "touching args: `one``two``three`", Args([]byte("–"), 4711, true)) fmt.Println() Expand(os.Stdout, "explicit `index:0` and `same:0`", Args(nil, 4711)) fmt.Println()
Output: want `arg1:#0/'arg1'` here and `arg2:#1/'arg2'` here template with backtick '``' and an `arg:#0/'arg'` here touching args: `one:4711``two:true``three:–` explicit `index:4711` and `same:4711`
Example (ExplicitIndex) ¶
var writeTestArg = func(wr io.Writer, idx int, name string) (int, error) { return fmt.Fprint(wr, idx) } Expand(os.Stdout, "`a`, `b:11`, `c`, `d:0`, `e`", writeTestArg)
Output: `a:0`, `b:11`, `c:1`, `d:0`, `e:2`
func Expands ¶
func Expands(tmpl string, writeArg ParamWriter) (string, error)
Expands uses Expand to return the expanded temaplate as a string.
func ExtractParams ¶
ExtractParams extracs the parameter names from template tmpl and appends them to appendTo.
func Parse ¶
Parse parses a sllm message create by Expand 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 m, _ := ParseMap( "added `count:7` ⨉ `item:Hat` to shopping cart by `user:John Doe`", &tmpl, ) fmt.Println(tmpl.String()) for k, v := range m { 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 IllegalArgIndex ¶
type IllegalArgIndex int
func (IllegalArgIndex) Error ¶
func (err IllegalArgIndex) Error() string
type ParamWriter ¶
ParamWriter is used by the Expand function to process an argument when it appears in the expand process of a template. Expand will pass the index idx and the name of the argument to expand, i.e. write into the writer wr. A ParamWriter returns the number of bytes writen and—just in case—an error.
NOTE The writer wr of type ValueEsc will escape whatever ParamWriter
writes to wr so that the template escape symbol '`' remains recognizable.
func Args ¶
func Args(u []byte, av ...interface{}) ParamWriter
Example ¶
Expand(os.Stdout, "just an `what`", Args(nil, "example"))
Output: just an `what:example`
Example (Undef) ¶
Expand(os.Stdout, "just an `what`: `miss`", Args([]byte("<undef>"), "example"))
Output: just an `what:example`: `miss:<undef>`
func Map ¶
func Map(u []byte, m ArgMap) ParamWriter
Example ¶
Expand(os.Stdout, "just an `what`", Map(nil, ArgMap{ "what": "example", "dummy": false, }))
Output: just an `what:example`
Example (Undef) ¶
Expand(os.Stdout, "just an `what`: `miss`", Map([]byte("<undef>"), ArgMap{ "what": "example", "dummy": false, }))
Output: just an `what:example`: `miss:<undef>`
type SyntaxError ¶
type SyntaxError struct { // Tmpl is the errornous template string Tmpl string // Pas is the byte position within the template string Pos int // Err is the description of the error Err string }
SyntaxError describes errors of the sllm template syntax in a message template.
func (SyntaxError) Error ¶
func (err SyntaxError) Error() string
type UndefinedArg ¶
type UndefinedArg string
func (UndefinedArg) Error ¶
func (err UndefinedArg) Error() string