Skip to content

k.logger ​

Server-side code logging and paginated log querying

Execution ContextPrerequisitesTypical Use
All server-side scripts (API, CodeBlock, Page, View, Jobs)Enabled by default (CodeLogSettings.Enable = true, LogLevel = Debug); gated by site CodeLogSettings and flushed asynchronouslyApplication 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:

js
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:

js
k.api.get("log-query", () => {
  return k.logger.query({
    category: "Store.Order",
    pageIndex: 1,
    pageSize: 10
  })
})

Quick Reference ​

Method / PropertyDescriptionReturns
debug([category, ]message)Write a Debug log entryvoid
information([category, ]message)Write an Information log entryvoid
warning([category, ]message)Write a Warning log entryvoid
error([category, ]message)Write an Error log entryvoid
critical([category, ]message)Write a Critical log entryvoid
query(arg)Query logs with date, category, level, keyword, or TraceId filtersCodeLogQueryResult

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.

ParameterTypeRequiredDescription
categorystringNoLog category (omit when calling the single-argument overload)
messagestringYesLog message text (serialize objects first)

Returns: void.

Note

  • message expects a string. Use JSON.stringify() when logging structured objects.
  • Written logs appear in the Kooboo admin under Development → Code Log.
js
// 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.

ParameterTypeRequiredDefaultDescription
categorystringNo-Log category filter
levelstringNo-Log level (such as Debug, Information, Warning, Error, Critical)
keywordstringNo-Case-sensitive substring filter matching both category and message
traceIdstringNo-Request trace ID
pageIndexnumberNo11-based page number
pageSizenumberNo20Items per page
startDateDate | stringNo-Start timestamp
endDateDate | stringNo-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):

FieldTypeDescription
listCodeLog[]Log entries on the current page
totalnumberTotal matching entries
pageIndexnumberCurrent 1-based page index
pageSizenumberItems per page
pageCountnumberTotal pages
js
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
ts
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;
}