@molang/alogjs
v0.1.5
Published
A small TypeScript logger matching github.com/molang-dev/alogjs.
Readme
alogjs
alogjs is a TypeScript port of github.com/molang-dev/alogjs.
It keeps the same logging model: package-level helpers, independent logger
instances, console output, daily file output, text/JSON formats, and optional
caller fields.
The default entry is browser-safe and does not import node:* modules. Use the
Node entry when you need file output.
Features
- Console logging.
- File logging.
- Daily log files named like
2026-07-11.log. - Configurable file output directory.
- Configurable daily file prefix, such as
app-2026-07-11.log. - Existing daily log files are appended to, not overwritten.
- Console and file output can be enabled at the same time.
- Text and JSON output formats can be configured separately for screen and file.
- Optional caller fields can be enabled only at or above a configured level.
- Package-level helpers such as
I(...). - Default singleton
log. - Independent logger instances through
ALog().
Quick Start
import { log } from '@molang/alog'
log.V('Startup', 'verbose message')
log.D('Startup', 'debug message')
log.I('Startup', 'info message')
log.W('Startup', 'warning message')
log.E('Startup', 'error message')
log.Fatal('Startup', 'fatal message and exit')Text output:
2026-07-11 13:55:34.386|V|12345|Startup|verbose message
2026-07-11 13:55:34.386|D|12345|Startup|debug message
2026-07-11 13:55:34.386|I|12345|Startup|info message
2026-07-11 13:55:34.386|W|12345|Startup|warning message
2026-07-11 13:55:34.386|E|12345|Startup|error message
2026-07-11 13:55:34.386|F|12345|Startup|fatal message and exitLogger Instances
import { ALog, LevelDebug } from '@molang/alog'
const logger = ALog()
logger.SetLevel(LevelDebug)
logger.D('Sync', 'loaded %d items', 10)Output Flags
import { FlagColor, FlagScreen, log } from '@molang/alog'
log.SetFlags(FlagScreen | FlagColor)
log.I('Main', 'screen')Available flags:
FlagScreen: write to the configured screen writer.FlagFile: write to the daily date file in the Node entry.FlagColor: colorize text screen output with ANSI colors.
The default flags are:
FlagScreen | FlagColorFile Output
import { ALog, FlagFile, FlagScreen } from '@molang/alog/node'
const logger = ALog()
logger.SetDir('./logs')
logger.SetFilePrefix('app-')
logger.SetFlags(FlagScreen | FlagFile)
logger.I('File', 'screen and ./logs/app-2026-07-11.log')This writes to:
./logs/app-2026-07-11.logThe directory is created when the first file log is written. Existing files are opened in append mode.
Format
The default text format is:
YYYY-MM-DD HH:mm:ss.SSS|Level|PID|Tag|MessageSet screen and file formats separately:
import { ALog, FormatJSON, FormatText } from '@molang/alog/node'
const logger = ALog()
logger.SetFormat(FormatText, FormatJSON)
logger.I('Startup', 'hello %s', 'world')JSON output is one object per line:
{"time":"2026-07-11 13:55:34.386","level":"I","pid":12345,"tag":"Startup","msg":"hello world"}If the message starts with a JSON object or array after leading whitespace and ends with the matching closing character before trailing whitespace, it is written as raw JSON:
logger.I('Price', ' { "price": "123" } ')
logger.I('Prices', ' [ {"price": "123"} ] '){"time":"2026-07-11 13:55:34.386","level":"I","pid":12345,"tag":"Price","msg": { "price": "123" } }
{"time":"2026-07-11 13:55:34.386","level":"I","pid":12345,"tag":"Prices","msg": [ {"price": "123"} ] }Only the outer shape is checked. Invalid raw JSON remains the caller's responsibility.
Caller Fields
Caller fields are disabled by default because they parse Error().stack.
import { ALog, FlagFunc, FlagShortFile, LevelWarning } from '@molang/alog'
const logger = ALog()
logger.SetCallerFlags(LevelWarning, FlagShortFile | FlagFunc)
logger.W('Sync', 'slow response')Example:
2026-07-11 13:55:34.386|W|12345|Sync|main.ts:23|main|slow responseTiming
const id = logger.Time()
// do work
logger.TimeEnd(id, 'Work', 'finished')TimeEnd writes a debug log with an elapsed= suffix.
Fatal Logs
Fatal writes the fatal log and writes the current stack trace. In the Node
entry it exits the process with status code 1; in the browser-safe entry it
throws an error.
