Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

JS: Logging and String Formatting

Logging and String Formatting

Source code: src/helpers.js, src/sprintf.js

Every writer takes a format and its arguments, in the style of printf. There is no gobj parameter and no error code, which the C API has. The JS runtime is simpler.


Write a log

log_error(format, ...args)

Writes an error. It goes to the remote handler too.

log_warning(format, ...args)

Writes a warning. It goes to the remote handler too.

log_info(format, ...args)

Writes an information message. It stays in the console.

log_debug(format, ...args)

Writes a debug message. It stays in the console.


Write a trace

trace_msg(format, ...args)

Writes one line of trace.

trace_json(json, msg)

Writes a JSON value.

Turn the levels on and off with the functions in Traces.


Where the logs go

set_remote_log_functions(remote_log_fn)

Sends the errors and the warnings to one handler, such as a websocket that carries them to a log centre. The information and the debug messages stay in the console.

set_log_callback(callback)

Sends every log to one function of the application.

set_console_log_enabled(enabled)

Turns the write to the console on or off.

set_console_log_filter(fn)

Gives one function the say over each console line, on top of the switch above. fn(level, msg) gives true to write the line. null writes them all, which is the default.

The console write occurs before the log callback runs, so nothing after it can remove a line. This is the only place to keep a class of lines off the console — for example, the timer traffic of the machine trace.

The filter decides the console only. The callback still gets every line. A filter that throws is ignored: a broken filter must not silence the log.


Format a string

sprintf(format, ...args)

Builds a string in the style of printf.

vsprintf(fmt, argv)

The same, and it takes the arguments in an array.

The conversions

ConversionMeaning
%sA string.
%d, %iAn integer.
%f, %e, %gA real number.
%o, %x, %XOctal and hexadecimal.
%bBinary.
%cOne character.
%jThe JSON form.
%tA boolean, as true or false.
%TThe name of the type.
%vThe value, with the type found automatically.
%uAn integer with no sign.