Logging
The loggerOptions
section within the KigoContext
allows you to configure logging behavior for your application. This includes setting the log level, providing a custom logger, and utilizing the built-in debug
package for managing log output directly in the browser.
Defaults
The code below illustrates setting the KigoContext
with the default values for loggerOptions
:
const kigoContext = {
loggerOptions: {
level: "error",
customLogger: undefined,
},
};
Properties
-
level
- Defines the level of logging to be used in the application.- Default:
"error"
- The logger will only capture errors by default. - Valid values:
"debug" | "info" | "warn" | "error"
- Example:
loggerOptions: { level: "debug" }
- Default:
-
customLogger
- Allows you to specify a custom logger implementation that adheres to theCustomLogger
interface.- Default:
undefined
- If set toundefined
, the default logging mechanism will be used. - Valid values:
CustomLogger
(an object implementing custom logging methods) - Example:
loggerOptions: { customLogger: new MyCustomLogger() }
- Default:
CustomLogger Interface
If you choose to implement a customLogger
, it should adhere to the following interface:
export interface CustomLogger {
log(level: LogLevel, logEntry: LogEntry): void;
}
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
export interface LogEntry {
message: string;
timestamp: string;
level: LogLevel;
namespace: string;
context?: Record<string, any>;
}
LogEntry Structure
The logEntry
object passed to log
contains the following properties:
message
: The log message.timestamp
: The timestamp of the log entry.level
: The level of the log ("debug"
,"info"
,"warn"
,"error"
).namespace
: The namespace associated with the log (e.g.,"kigo-sdk:info"
).context
: (Optional) Additional context or metadata associated with the log entry.
The Debug Package
The logging functionality in the KigoContext
uses the debug
package under the hood. This package provides a simple way to manage logging output in the browser. You can enable or disable specific logging levels by setting the localStorage.debug
property.
Enabling/Disabling Logging in the Browser
To manage logging levels:
-
Enable all logs:
localStorage.debug = 'kigo-sdk:*';
-
Enable only error logs:
localStorage.debug = 'kigo-sdk:error';
-
Disable all logs:
localStorage.debug = '';
After setting the localStorage.debug
value, refresh the page for the changes to take effect.
For more detailed information on how to use the debug
package, please refer to the debug documentation.
Updated 8 months ago