src/shobiz

Source   Edit  

Sho is a simple structured console output library, meant to be used with minimal runtime configuration (e.g. --debug and --pretty flags).

Debug messages are only shown when the SHO_DEBUG flag is set. The output can be pretty printed by setting the SHO_PRETTY flag for easier reading, but by default, the output is minified single-line JSON which can be easily parsed by other tools (e.g. jq) or piped to a file (e.g. > file.jsonl).

Vars

SHO_DEBUG = false
Print debug information. Source   Edit  
SHO_INDENT = 4
Indentation level for pretty printing. Source   Edit  
SHO_OUTPUT = stdout
Where to write the output. Source   Edit  
SHO_PRETTY = false
Pretty print the output. Source   Edit  
SHO_TIMEFMT = "yyyy-MM-dd\'T\'HH:mm:ss\'.\'fffzzz"
Time format (default ISO8601). Source   Edit  
SHO_USEUTC = false
Use UTC time. Source   Edit  

Procs

proc shoDbg(msg: string) {....raises: [TimeFormatParseError, IOError, ValueError],
                           tags: [TimeEffect, WriteIOEffect], forbids: [].}
Show a debug message. This message will only be shown if the SHO_DEBUG flag is set.

Example:

SHO_PRETTY = true
SHO_DEBUG = true

"Detailed message.".shoDbg()

Produces:

{
    "timestamp": "2024-05-27T21:57:43.862-07:00",
    "level": "Debug",
    "message": "Detailed message."
}

Source   Edit  
proc shoDbg(msg: string; data: JsonNode) {.
    ...raises: [TimeFormatParseError, IOError, ValueError],
    tags: [TimeEffect, WriteIOEffect], forbids: [].}
Show a debug message with additional data. This message will only be shown if the SHO_DEBUG flag is set.

Example:

import json

SHO_PRETTY = true
SHO_DEBUG = true

"Detailed message.".shoDbg(%*{"key": "value"})

Produces:

{
    "timestamp": "2024-05-27T21:57:43.862-07:00",
    "level": "Debug",
    "message": "Detailed message.",
    "data": {
        "key": "value"
    }
}

Source   Edit  
proc shoExc(err: ref Exception) {....raises: [TimeFormatParseError, IOError,
    ValueError], tags: [TimeEffect, WriteIOEffect], forbids: [].}
Show an exception message.

Example:

SHO_PRETTY = true

try:
  raise newException(KeyError, "error")
except Exception as err:
  err.shoExc()

Produces:

{
    "timestamp": "2024-05-27T21:57:43.862-07:00",
    "level": "Error",
    "type": "KeyError",
    "message": "error"
}

Source   Edit  
proc shoExc(err: ref Exception; data: JsonNode) {.
    ...raises: [TimeFormatParseError, IOError, ValueError],
    tags: [TimeEffect, WriteIOEffect], forbids: [].}
Show an exception message with additional data.

Example:

import json

SHO_PRETTY = true

try:
  raise newException(KeyError, "error")
except Exception as err:
  err.shoExc(%*{"key": "value"})

Produces:

{
    "timestamp": "2024-05-27T21:57:43.862-07:00",
    "level": "Error",
    "type": "KeyError",
    "message": "error",
    "data": {
        "key": "value"
    }
}

Source   Edit  
proc shoMsg(msg: string) {....raises: [TimeFormatParseError, IOError, ValueError],
                           tags: [TimeEffect, WriteIOEffect], forbids: [].}
Show a message.

Example:

SHO_PRETTY = true

"Hello, World!".shoMsg()

Produces:

{
    "timestamp": "2024-05-27T21:57:43.862-07:00",
    "level": "Message",
    "message": "Hello, World!"
}

Source   Edit  
proc shoMsg(msg: string; data: JsonNode) {.
    ...raises: [TimeFormatParseError, IOError, ValueError],
    tags: [TimeEffect, WriteIOEffect], forbids: [].}
Show a message with additional data.

Example:

import json

SHO_PRETTY = true

"Hello, World!".shoMsg(%*{"key": "value"})

Produces:

{
    "timestamp": "2024-05-27T21:57:43.862-07:00",
    "level": "Message",
    "message": "Hello, World!",
    "data": {
        "key": "value"
    }
}

Source   Edit