Skip to content
Anssi Halmeaho edited this page Apr 29, 2020 · 2 revisions

stdlog

Logging services. Logger is serializing concurrent log writings via one buffer. Logger calls procedure given by client which may print log, store it to database etc. Log writing is sequence of data items.

get-logger

Creates logger which uses procedure (or external procedure) given as argument.

type: procedure

Arguments:

  1. log handler procedure: this is called for handling log writing
  2. optionally map which consists options for logger

Logger options (map keys):

Key Meaning
'buffer-size' int, buffer size for buffering log writings (default is 1024)

Format:

call(stdlog.get-logger <log-handler-procedure>) -> <logger-external-procedure>
call(stdlog.get-logger <log-handler-procedure> <map-options>) -> <logger-external-procedure>

Return value: external procedure (used for log writing)

logger-procedure format (for writing log):

call(<logger-procedure> <arg-1> <arg-2> ...) -> true

Example:

import stdlog

my-log = proc(item)
  import stdio
  call(stdio.printline 'my-log: ' item)
end

#log = call(stdlog.get-logger my-log map('buffer-size' 100))
log = call(stdlog.get-logger my-log)

_ = call(log 'something happened' 10 20)
_ = call(log list(1 2 3))
_ = call(log 'ok')
_ = call(log 'fail')

import stdtime
call(stdtime.sleep 1)

->
my-log: list('something happened', 10, 20)
my-log: list(list(1, 2, 3))
my-log: list('ok')
my-log: list('fail')

get-default-logger

Creates logger which is wrppaer for Go's standard library logger. Options can be given in map as argument (optional). Logger writes to standard error.

type: procedure

Logger options (map keys):

Key Meaning
'prefix' prefix string for each log writing (defualt is none)
'separator' fiels separator (':' is default)
'date' if true date is written (default is false)
'time' if true time is written (default is false)
'microseconds' if true microseconds is written (default is false)
'UTC' if true date and time is written in UTC format (default is false)

Format:

call(stdlog.get-default-logger) -> <logger-external-procedure>
call(stdlog.get-logger <map-options>) -> <logger-external-procedure>

Return value: external procedure (used for log writing)

logger-procedure format (for writing log):

call(<logger-procedure> <arg-1> <arg-2> ...) -> true

Example: No options map given

log = call(stdlog.get-default-logger)
_ = call(log 'something happened' 10 20)

->
:'something happened':10:20

Example: Options map given

options = map(
  'prefix'       'MYLOG: '
  'separator'    ' | '
  'date'         true
  'time'         true
  'microseconds' true
  'UTC'          true
)
log = call(stdlog.get-default-logger options)
_ = call(log 'something happened' 10 20)

->
MYLOG: 2020/04/29 17:09:45.146485  | 'something happened' | 10 | 20
Clone this wiki locally