1 Commits
js-ic ... log

Author SHA1 Message Date
John Alanbrook
e98abcdda7 initial logging try
Some checks failed
Build and Deploy / build-macos (push) Failing after 5s
Build and Deploy / build-windows (CLANG64) (push) Has been cancelled
Build and Deploy / package-dist (push) Has been cancelled
Build and Deploy / deploy-itch (push) Has been cancelled
Build and Deploy / deploy-gitea (push) Has been cancelled
Build and Deploy / build-linux (push) Has been cancelled
2025-05-24 22:01:21 -05:00
3 changed files with 139 additions and 0 deletions

20
.moth/log Normal file
View File

@@ -0,0 +1,20 @@
{
"console": {
"outputs": ["console"]
},
"info": {
"outputs": ["file:logs/info.log"]
},
"warning": {
"outputs": ["console", "file:logs/warnings.log"]
},
"error": {
"outputs": ["console", "file:logs/errors.log", "actor:localhost:5678"]
},
"debug": {
"outputs": ["file:logs/debug.log"]
},
"audit": {
"outputs": ["file:logs/audit.log", "actor:audit-server:9000"]
}
}

View File

@@ -166,6 +166,96 @@ console[prosperon.DOC] = {
clear: "Clear console." clear: "Clear console."
} }
// Initialize log system
var logConfig = {}
var logPath = '.moth/log'
// Default configuration if no .moth/log exists
var defaultLogConfig = {
console: { outputs: ['console'] }
}
// Try to load log configuration
if (io.exists(logPath)) {
try {
var logContent = io.slurp(logPath)
logConfig = JSON.parse(logContent)
} catch (e) {
console.warn(`Failed to parse ${logPath}, using default configuration: ${e.message}`)
logConfig = defaultLogConfig
}
} else {
logConfig = defaultLogConfig
}
// Create global log object
globalThis.log = {}
// Helper function to create a log function for a specific log level
function createLogFunction(logName, config) {
return function(expr) {
// If logging is not enabled for this level, do nothing
if (!config || !config.outputs || config.outputs.length === 0) return
var timestamp = time.text(time.now(), "mb d yyyy h:nn:ss")
var message = `[${logName}] ${timestamp}: ${String(expr)}`
// Process each output destination
for (var output of config.outputs) {
if (output === 'console') {
// Output to console
console.print(message + '\n')
} else if (output.startsWith('file:')) {
// Output to file
var filename = output.substring(5)
try {
var existing = io.exists(filename) ? io.slurp(filename) : ''
io.slurpwrite(filename, existing + message + '\n')
} catch (e) {
console.error(`Failed to write to log file ${filename}: ${e.message}`)
}
} else if (output.startsWith('actor:')) {
// Send to actor
var parts = output.substring(6).split(':')
var address = parts[0]
var port = parseInt(parts[1])
// Create an actor reference with the address and port
var logActor = create_actor({
address: address,
port: port
})
// Send log message to the actor (defer if send not available yet)
try {
// Check if send is available
if (typeof send !== 'undefined') {
send(logActor, {
type: 'log',
level: logName,
message: expr,
timestamp: timestamp,
source: prosperon.id
})
} else {
// If send is not available yet, just print to console as fallback
console.print(`[${logName}] (actor output pending): ${expr}\n`)
}
} catch (e) {
console.error(`Failed to send log to actor ${address}:${port}: ${e.message}`)
}
} else {
console.warn(`Unknown log output type: ${output}`)
}
}
}
}
// Create log functions for each configured log level
for (var logName in logConfig) {
log[logName] = createLogFunction(logName, logConfig[logName])
}
var BASEPATH = 'scripts/core/base.js' var BASEPATH = 'scripts/core/base.js'
var script = io.slurp(BASEPATH) var script = io.slurp(BASEPATH)
var fnname = "base" var fnname = "base"

29
test_log.js Normal file
View File

@@ -0,0 +1,29 @@
// Test script for the log statement system
// Test console logging
log.console("This is a console log message");
// Test info logging (goes to file)
log.info("Application started");
log.info("User logged in: user123");
// Test warning logging (goes to console and file)
log.warning("Memory usage is high");
// Test error logging (goes to console, file, and actor)
log.error("Failed to connect to database");
// Test debug logging (goes to file only)
log.debug("index: " + 42);
log.debug("Processing item: " + JSON.stringify({id: 1, name: "test"}));
// Test audit logging (goes to file and actor)
if (log.audit) {
log.audit("User performed sensitive action");
}
// Test with expressions as per the spec
var index = 5;
log.debug("index value is: " + index);
console.log("Log test completed. Check console output and log files.");