Logging in Scilab
Here’s the translation of the Go logging example to Scilab:
Our logging example demonstrates various ways to output logs in Scilab. While Scilab doesn’t have built-in logging packages like Go’s log
and slog
, we can achieve similar functionality using Scilab’s built-in functions and some custom implementations.
// Function to get current timestamp
function ts = get_timestamp()
t = getdate();
ts = sprintf("%04d/%02d/%02d %02d:%02d:%02d", t(1), t(2), t(6), t(7), t(8), t(9));
endfunction
// Simple logging function
function log_print(message)
mprintf("%s %s\n", get_timestamp(), message);
endfunction
// Logging with microsecond precision
function log_print_micro(message)
t = getdate();
mprintf("%s.%06d %s\n", get_timestamp(), t(10), message);
endfunction
// Custom logger with prefix
function mylog = create_logger(prefix)
function log_message(message)
mprintf("%s%s %s\n", prefix, get_timestamp(), message);
endfunction
mylog = log_message;
endfunction
// Main execution
log_print("standard logger");
log_print_micro("with micro");
// Scilab doesn't have built-in file/line logging, so we'll skip that example
mylog = create_logger("my:");
mylog("from mylog");
mylog = create_logger("ohmy:");
mylog("from mylog");
// Logging to a string (similar to bytes.Buffer in Go)
function [log_str, logger] = create_string_logger(prefix)
log_str = "";
function log_message(message)
global log_str;
log_str = log_str + sprintf("%s%s %s\n", prefix, get_timestamp(), message);
endfunction
logger = log_message;
endfunction
[buf, buflog] = create_string_logger("buf:");
buflog("hello");
mprintf("from buflog:%s", buf);
// Scilab doesn't have built-in JSON logging, so we'll create a simple key-value logger
function log_info(message, varargin)
ts = get_timestamp();
mprintf('{"time":"%s","level":"INFO","msg":"%s"', ts, message);
for i = 1:2:length(varargin)
mprintf(',"%s":"%s"', string(varargin(i)), string(varargin(i+1)));
end
mprintf("}\n");
endfunction
log_info("hi there");
log_info("hello again", "key", "val", "age", 25);
This Scilab code demonstrates various logging techniques:
- We start by creating a simple timestamp function and basic logging functions.
- We then create a function to log with microsecond precision.
- A custom logger with a prefix is implemented using a closure.
- We demonstrate logging to a string, which is similar to logging to a buffer in Go.
- Finally, we create a simple key-value logger that outputs in a JSON-like format.
To run this program, save it as logging.sce
and execute it using Scilab:
$ scilab -nw -f logging.sce
The output will look similar to this (the actual timestamps will vary):
2023/08/22 10:45:16 standard logger
2023/08/22 10:45:16.904141 with micro
my:2023/08/22 10:45:16 from mylog
ohmy:2023/08/22 10:45:16 from mylog
from buflog:buf:2023/08/22 10:45:16 hello
{"time":"2023-08-22 10:45:16","level":"INFO","msg":"hi there"}
{"time":"2023-08-22 10:45:16","level":"INFO","msg":"hello again","key":"val","age":"25"}
Note that Scilab doesn’t have built-in support for some features like file/line logging or structured JSON logging, so we’ve adapted the example to demonstrate similar concepts using Scilab’s capabilities.