k.logger
Server-side code logging and paginated log querying
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| All server-side scripts (API, CodeBlock, Page, View, Jobs) | Enabled by default (CodeLogSettings.Enable = true, LogLevel = Debug); gated by site CodeLogSettings and flushed asynchronously | Application diagnostics, error tracking, and TraceId auditing |
k.logger provides server-side logging and paginated querying across five severity levels: debug, information, warning, error, and critical. Each level accepts either a single message argument or (category, message). Logging is gated by the site's CodeLogSettings (Enable and LogLevel threshold—entries below the configured level or when disabled are silently dropped) and is flushed asynchronously by a background queue every ~500ms into weekly CodeLog storage.
Quick Start
Asynchronous Flush Latency
k.logger.* enqueues entries into an in-memory background queue that flushes to the current week's CodeLog storage roughly every 500ms. Because writes are asynchronous, calling k.logger.query() immediately in the same request will not return entries written during that request.
Write log entries:
k.api.post("log-demo", () => {
k.logger.information("Store.Order", "Created order #1024")
k.logger.error("Store.Order", JSON.stringify({ orderId: 1024, reason: "timeout" }))
return { logged: true }
})Query entries from the current week in a separate request:
k.api.get("log-query", () => {
return k.logger.query({
category: "Store.Order",
pageIndex: 1,
pageSize: 10
})
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
debug([category, ]message) | Write a Debug log entry | void |
information([category, ]message) | Write an Information log entry | void |
warning([category, ]message) | Write a Warning log entry | void |
error([category, ]message) | Write an Error log entry | void |
critical([category, ]message) | Write a Critical log entry | void |
query(arg) | Query logs with date, category, level, keyword, or TraceId filters | CodeLogQueryResult |
Methods
debug() / information() / warning() / error() / critical()
Write a log entry at the chosen severity level; pass two arguments (category, message) to categorize the entry or one argument (message) to omit the category.
| Parameter | Type | Required | Description |
|---|---|---|---|
category | string | No | Log category (omit when calling the single-argument overload) |
message | string | Yes | Log message text (serialize objects first) |
Returns: void.
Note
messageexpects astring. UseJSON.stringify()when logging structured objects.- Written logs appear in the Kooboo admin under Development → Code Log.
// Debug level
k.logger.debug("Store.Order", "Order xxx created")
// Information level
k.logger.information("Store.Order", "Order xxx created")
// Warning level
k.logger.warning("Store.Order", "Order xxx not set create date!")
// Error level
k.logger.error("Store.Order", "Order xxx not found!")
// Critical level
k.logger.critical("Application", "Application crash!")
// Single-argument overload (empty category)
k.logger.information("Single param info")
// Serialize objects before logging
k.logger.error("Store.Order", JSON.stringify({ id: 1, message: "Order not found" }))query()
Query stored code logs with optional filters and pagination. All fields on CodeLogQuery are optional (calling k.logger.query({}) defaults to pageIndex: 1, pageSize: 20). Note that k.logger.query() always searches only the current week's CodeLog store (siteDb.CodeLog); passing startDate / endDate from earlier weeks will not read prior weekly log folders.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
category | string | No | - | Log category filter |
level | string | No | - | Log level (such as Debug, Information, Warning, Error, Critical) |
keyword | string | No | - | Case-sensitive substring filter matching both category and message |
traceId | string | No | - | Request trace ID |
pageIndex | number | No | 1 | 1-based page number |
pageSize | number | No | 20 | Items per page |
startDate | Date | string | No | - | Start timestamp |
endDate | Date | string | No | - | End timestamp |
Returns: PagedList<CodeLog> (CodeLogQueryResult) paged log result object containing these fields (note: each returned CodeLog item only populates creationDate, message, level, category, and traceId; id, name, statusCode, and lastModified remain at their defaults):
| Field | Type | Description |
|---|---|---|
list | CodeLog[] | Log entries on the current page |
total | number | Total matching entries |
pageIndex | number | Current 1-based page index |
pageSize | number | Items per page |
pageCount | number | Total pages |
k.api.get("query", () => {
const now = new Date()
const start = new Date(now.getTime() - 24 * 60 * 60 * 1000)
return k.logger.query({
startDate: start.toISOString(),
endDate: now.toISOString(),
category: "Store.Order",
level: "Error",
pageIndex: 1,
pageSize: 100,
keyword: "timeout",
traceId: "0HMCP7T6K7BFS:00000002"
})
})TypeScript Definitions
Show full TypeScript definitions
interface KLogger {
debug(message: string): void;
debug(category: string, message: string): void;
information(message: string): void;
information(category: string, message: string): void;
warning(message: string): void;
warning(category: string, message: string): void;
error(message: string): void;
error(category: string, message: string): void;
critical(message: string): void;
critical(category: string, message: string): void;
query(arg: CodeLogQuery): CodeLogQueryResult;
}
interface CodeLogQuery {
startDate?: Date | string;
endDate?: Date | string;
category?: string;
level?: string;
keyword?: string;
traceId?: string;
pageIndex?: number;
pageSize?: number;
}
interface PagedList<T> {
list: T[];
total: number;
pageIndex: number;
pageSize: number;
pageCount: number;
}
type CodeLogQueryResult = PagedList<CodeLog>;
interface CodeLog {
id: number;
name: string;
category: string;
level: string;
message: string;
statusCode: number;
traceId: string;
constType: number;
creationDate: Date;
lastModified: Date;
}Related Docs
- KScript Language and Runtime — Uncaught errors and code logging
- k.api — Register API endpoints