AnyLogger is a simple open-source, no dependencies, JavaScript logger library, that can be extended and modified to fit most of your logging needs. AnyLogger is available in a lightweight (~1.8kb min+gzip) anyLogger.js or extended anyLoggerMax.js versions.
- Console Logging
- HTML Logging (AnyLoggerMax only)
- Service Logging (AnyLoggerMax only)
- Global Log Collecting
- Log Capture
- Formatter
- Handlers
- Plugins
- Quick Start
- Demo
- Settings
- Console Logging
- HTML Logging
- Service Logging
- Global Log Collecting
- Log Capture
- Formatter
- Handlers
- Plugins
- API
Add AnyLogger or AnyLoggerMax file to your project and consume it through <script> or AMD / CommonJS loader. Next, AnyLogger is ready to crate a logger instance and start logging. It comes with default behavior, but you might want to add some setup settings.
<html>
<body>
<script type="text/javascript" src="./anyLogger.js"></script>
<script type="text/javascript">
var loggerInst = AnyLogger.create();
loggerInst.debug('hello world');
</script>
</body>
</html>Check out this demo page or the folders demoLight and demoMax to see AnyLogger in action.
AnyLogger accept setup settings in the 'create' call. Most of the settings can be changed on a later stage using the API.
AnyLogger.create(settings);| Setting | Value | Description |
|---|---|---|
| id | string | logger instance id |
| logLevel | 'debug', 'info', 'warn', 'error', 'off' | set the log level from which you want to see logs |
| module | string | set the module for the log messages |
| formatter | function | function that receives a string message and a data object and returns a formatted string message |
| handlers | array of handlers | array of handlers which do the actual logging work |
| useFormatter | boolean | configure if to use the default formatter in case no custom formatter was set |
| collect | boolean | configure if to collect global errors and console logs |
| captureLogs | boolean | configure if to capture logs or not |
| captureLogsLimit | integer | set the limit of how many captured logs to store |
| flushCapturedLogsOnError | {handlerTypes:[''],logLevel:''} | configure if to flush all the captured logs, when an error level is logged |
| flushCapturedLogsOnLimit | {handlerTypes:[''],logLevel:''} | configure if to flush all the captured logs, when it reaches the limit |
| logToConsole | boolean | configure if to use the console handler or not |
| logToHtml (Max only) | {container:''} | configure if to use the HTML handler (available only on AnyLoggerMax) or not and which HTML container to use |
| logToService (Max only) | {loggingUrl:'', batchSize:integer, flushOnWindowClose:boolean, headers: [{'':''}]} | configure if to use Service handler (available only on AnyLoggerMax) or not and set it's configuration |
Default handler that logs messages to the console
Handler that logs to an HTML table that can be filtered, sorted and cleared. Very useful for mobile devices. Only available in AnyLoggerMax.
Handler that logs to a server through an HTTP request. You can configure the service URL, request headers, batch size of the logs on each request and if to flush the remaining logs on window close event. Very useful for production monitoring. Only available in AnyLoggerMax.
AnyLogger can collect global errors / exceptions (window.onerror) and console logs and route them through the handlers. Very useful for investigating environment / system related errors as well as unhandled code.
AnyLogger can store logs until it suites you to flush them with flushCapturedLogs. You can configure the limit of how many logs to store using captureLogsLimit and if to flush them when the limit is reached flushCapturedLogsOnLimit or when en error level log is received flushCapturedLogsOnError. Very useful for mobile devices and if you want to keep your console clean.
Formatter is a simple function that receives a string message and data object as input. It returns a formatted string message that will be used as an input message to the handlers. All non-collected messages are routed through the formatter. AnyLogger comes with a default formatter that can be disabled with 'useFormatter' configuration.
logger.create({
formatter: function(message, data){return "[" + data.module + "][" + data.scope + "]" + message},
module: 'moduleName',
logLevel: 'debug',
});Handlers are objects which provides a 'write' function that receives a string message, log level and data. According to configuration, log messages are routed through the handlers so they can do some logging work with them. Handlers can provide their own API that would be available when retrieving their instance through getHandlerByType. AnyLogger comes with one default 'console' handler and AnyLoggerMax also comes with 'html' and 'service' handlers. You can provide your own custom handler and set it with addHandler or with settings. You can set the handler as a function or an object.
var loggerInst = logger.create({
handlers: [handlers: [function(message, level, data){console.debug(message)}],
module: 'moduleName',
logLevel: 'debug',
});
loggerInst.addHandler({
type: 'customHandler',
write: function(message, level, data) {
//do somthing
},
apiFunc1: function(){//do somthing},
});Plugins are module objects, which provides a 'create' function that receives the AnyLogger class and the provided settings. Plugins can manipulate AnyLogger freely, with complete access to it's infrastructure. You can provide your own custom plugin and set it with addPlugin
anyLogger.addPlugin({create: function(anyLoggerClass, setting){
//do somthing
}});Returns AnyLogger instance to start logging.
- settingsObj - (optional) setup Settings object.
retrieves AnyLogger instance by id.
- id - id of the instance.
registers a plugin class.
- plugin - plugin object with create function.
Logs the provided message with formatting and handling according to the data.
- message - string message data - (optional) object that can contain the properties 'module' and 'scope'.
Sets the logging level. Returns the log level.
- level - (optional) logLevel string or object.
turn log capture on / off.
- capture - boolean value.
set the limit of how many captured logs to store.
- limit - integer value.
configures if to flush all the captured logs when it reaches the limit.
- flushOnLimit - object that contains the properties 'logLevel': the minimum level of logs you want to flush and 'handlerTypes': which handlers you want to log to.
configures if to flush all the captured logs, when an error level is logged. This is useful if you only interested in investigating errors and need extended data of the logs that could guide to that error.
- flushOnError - object that contains the properties 'logLevel': the minimum level of logs you want to flush and 'handlerTypes': which handlers you want to log to.
return the captured logs.
flush all the captured logs.
- logLevel- the minimum level of logs you want to flush. handlerTypes- array of handler types you want to log to.
adds an handler to log messages to.
- handler- object that contains the properties 'type': string of the handler type(name) and 'write': function that receives a string message, log level and data.
return an handler instance by it's type.
- type- string of an handler type.