angular-logger2
// this example: resuable logging service, which overrides all static defaults and provides own logging contexts ; ls.getInstance'banana'.info'Hello World!'; // ignored, logging turned off for '*'ls.getInstance'main.subA'.info'Hello World!'; // ignored, doesn't pass logging threshold of 'main'ls.getInstance'main.subB'.trace'Hello World!'; // <17-5-2015 11:52:52::[main.subB]> Hello World!ls.getInstance'main.subB'.info'Hello %s!', 'World', ;// <3-6-2017 11:53:51::[main.subB]> Hello World! Object { "extra": "pass-through params"} // or java style:
WORKING DEMO
- About
- Installing
- Getting Started
- Applying Patterns - Prefix pattern - Datetime stamp patterns - Logging patterns
- Managing logging priority
- Reusable LoggingService instance outside of Angular
About
This library gives your angular app proper logging capabilities (and non-angular apps for simple usage). Although you can configure patterns and logging level priorities, it also simply works as a simple drop-in completely configured with sensible defaults (demo).
- Gives you Logging API, so that you can define separate contexts to log for, where the output will be prepended with the context's name, a datetime stamp and can be furter expanded to include the logging level.
- Further enhances the logging functions so that you can apply patterns eliminating the need of manually concatenating your strings
- Introduces log levels, where you can manage logging output per context or even a group of contexts
- Works as a injectable or standalone service or as a complete drop-in replacement for your current [
console.log
statements][console-logger]
Installing
npm install angular-logger2 --save
angular-logger2 has optional dependencies on momentjs and sprintf.js: without moment you can't pattern a nicely readable datetime stamp and without sprintf you can't pattern your logging input lines. Default fixed patterns are applied if either they are missing.
npm install sprintf-js --save
npm install moment --save
Getting Started
Importing the library types
First you need to decide if you wish to use the commonjs, esmodule or UMD module versions. The demo uses commonjs:
import { LoggingService, LoggingConfig, ContextLogLevel, LogLevel } from 'angular-logger2/dist-commonjs/index.js';
Configure defaults... or not
Every level of API gives an opertunity to provide defauts overriding values on levels below. You can define no config at all, define config for a LoggingService
instance or define config when getting a single Logger
instance.
// For example the following calls all use the static default of the lowest level:LoggingService.getLogger;new LoggingService.getLogger; // Next, overriding *some* defaults with new defaults one level higher (undefined leaves previous default in):new LoggingServicenew LoggingConfig'new default prefixpattern', undefined, undefined, customLogLevels.getLogger; // Again override *some* default with new defaults one level higher:LoggingService.getLogger'custom context', 'custom prefixpattern';myLoggingService.getLogger'custom context', 'custom prefixpattern';
Configure injectable reusable LoggingService instance in Angular context
-
Add LoggingService as a provider in your module:
;;;; -
Optionally add default config to override to static sensible defaults:
providers: , -
Have the LoggingService injected in your service or component by Angular:
import {Component} from "@angular/core"; import {LoggingService} from "angular-logger2/dist-esmodule"; @Component({ template: '' }) export class YourComponent { constructor(protected readonly loggingService: LoggingService) { this.loggingService.getLogger().warn('yay'); } }
Applying Patterns
Prefix pattern
By default, the prefix is formatted like so (if sprintf-js is present):
datetime here::context name here>your logging input here // if sprintf.js is missing, angular-logger2 defaults back to a fixed pattern that does include the used loglevel:datetime here::context name here::loglevel used here>your logging input here
However, the patter is very flexible. For example, you can change this as follows:
// prefix pattern '%s - %s: 'loggingService; // was: Sunday 12:55:07 am::[app]>Hello World// became: Sunday 12:55:07 am - app: Hello World
And if you add another prefix-placeholder %s
, you get the log level as well:
// pattern '%s - %s - %s: ' -> Sunday 12:55:07 am - app - info: Hello World
You can also remove it completely, or have just the datetime stamp or just the context prefixed:
// by the power of sprintf!// '%s - %s: ' -> both// '%s: ' -> timestamp// '%1$s: ' -> timestamp by index// '%2$s: ' -> context by index// '%2$s - %1$s: ' -> both, reversed// '%3$s: ' -> logging level by index// '%3$s - %2$s - %1$s: ' -> loglevel, context and timestamp reversed
This works because angular-logger2 will use three arguments context, timestamp and loglevel for the prefix, which can be referenced by index.
You can even align the values within the pattern using sprintf. Here's a complete example of all possibilities:
// '%1$s %3$5s %2$-30s' ->// June 4, 2017 10:16 AM info MyService In this example the loglevels are aligned right, with padding// June 4, 2017 10:16 AM error MyReader The contexts (class names in this case) are aligned left// June 4, 2017 10:16 AM info MyVeryLongServiceFactoryName Also includes the loglevel (3rd '%s'), switched places with the context
pattern | explanation |
---|---|
%s | indicates a string format |
%2$s | indicates a string format with argument 2 |
%2$30s | indicates a string format with argument 2 padding to be minimally 30 long |
%2$-30s | indicates a string format with argument 2 padding to be minimally 30 long, content aligned to the left |
Datetime stamp patterns
If you have included momentjs in your dependencies, you can start using datetime stamp patterns with angular-logger2. The default pattern is LLL
, which translates to a localized string that matches the current user's Locale. You customize the pattern as follows:
// datetimePattern = 'dddd';myLoggingService.getInstance'app'.info'Hello World'; // was: Sunday 12:55:07 am::[app]>Hello World// became: Sunday::[app]>Hello World
A pattern like dddd h:mm:ss a
would translate to something like "Sunday 12:55:07 am". You can easily switch to a 24h format as well, using these patterns.
- For all options, see moment.js
Logging patterns
If you have included sprintf-js in your dependencies, you can start using patterns with angular-logger2.
Traditional style with $console
:
console// Error uploading document [contract.pdf], Error: 'Service currently down'. Try again later. "{ ... }"
Modern style with angular-logger2:
;logger.error"Error uploading document [%s], Error: '%s'. Try again later.", filename, err.message// Sunday 12:13:06 pm::[myapp.file-upload]> Error uploading document [contract.pdf], Error: 'Service currently down'. Try again later.
You can even combine pattern input and normal input:
;logger.warn"This %s pattern %j", "is", "{ 'in': 'put' }", "but this is not!", , ;// 17-5-2015 00:16:08::[test]> This is pattern "{ 'in': 'put' }" but this is not! ["this", "is handled", "by the browser"] Object {including: "syntax highlighting", and: "console interaction"}
To log an Object
, you now have three ways of doing it, but the combined solution shown above has best integration with the browser.
logger; // json string with stringify's limitationslogger; // json string with sprintf's limitationslogger; // interactive tree in the browser with syntax highlightinglogger; logger;logger;
- For all options, see sprintf.js
Managing logging priority
Using logging levels, we can manage output on several levels. Contexts can be named using dot '.' notation, where the names before dots are interpreted as groups or packages.
For example for a.b
and a.c
we can define a general log level for a
and have a different log level for only a.c
.
The following logging functions (left side) are available:
logging function | mapped to: | with logLevel |
---|---|---|
logger.trace |
console.trace |
TRACE |
logger.debug |
console.debug |
DEBUG |
logger.info |
console.info |
INFO |
logger.warn |
console.warn |
WARN |
logger.error |
console.error |
ERROR |
The level's order are as follows:
1. TRACE: displays all levels, is the finest output and only recommended during debugging
2. DEBUG: display all but the finest logs, only recommended during develop stages
3. INFO : Show info, warn and error messages
4. WARN : Show warn and error messages
5. ERROR: Show only error messages.
6. OFF : Disable all logging, recommended for silencing noisy logging during debugging. *will* surpress errors logging.
Example:
// config log levels before the application wakes up; // globally only INFO and more important are logged// for group 'a' default is WARN and ERROR// a.b.c and a.b.d override logging everything-with-TRACE and least-with-ERROR respectively
LoggingService
instance outside of Angular
Reusable You can have an injectable LoggingService
instance configured as provider, but that doesn't mean you can't use the same instance outside of Angular.
Here's one example how to have a one single truth:
/// defaultLoggingService.ts; ; ;;
/// YourAppModule.ts;
/// YourNonAngularClass.ts; ;// or: