What is @sap-ux/logger?
@sap-ux/logger is an npm package designed to provide logging functionalities for SAP UX applications. It allows developers to log messages with different severity levels, format log messages, and manage log outputs efficiently.
What are @sap-ux/logger's main functionalities?
Basic Logging
This feature allows you to log messages with different severity levels such as info, warn, and error.
const { Logger } = require('@sap-ux/logger');
const logger = new Logger('myApp');
logger.info('This is an info message');
logger.warn('This is a warning message');
logger.error('This is an error message');
Custom Log Levels
This feature allows you to set custom log levels, enabling more granular control over what gets logged.
const { Logger, LogLevel } = require('@sap-ux/logger');
const logger = new Logger('myApp', { level: LogLevel.DEBUG });
logger.debug('This is a debug message');
Log Formatting
This feature allows you to format log messages, including options for timestamps and log levels.
const { Logger, LogFormatter } = require('@sap-ux/logger');
const formatter = new LogFormatter({ timestamp: true, level: true });
const logger = new Logger('myApp', { formatter });
logger.info('This is a formatted info message');
Log Output Management
This feature allows you to manage log outputs, such as writing logs to a file.
const { Logger, LogOutput } = require('@sap-ux/logger');
const output = new LogOutput({ filePath: 'logs/app.log' });
const logger = new Logger('myApp', { output });
logger.info('This message will be written to a file');
Other packages similar to @sap-ux/logger
winston
Winston is a versatile logging library for Node.js that supports multiple transports (e.g., console, file, HTTP) and log levels. It is highly configurable and widely used in the Node.js community. Compared to @sap-ux/logger, Winston offers more flexibility and a broader range of features.
bunyan
Bunyan is a simple and fast JSON logging library for Node.js services. It provides a structured logging approach and supports various log levels and outputs. Bunyan is known for its performance and ease of use. Compared to @sap-ux/logger, Bunyan focuses more on JSON-based structured logging.
pino
Pino is a low-overhead, high-performance logging library for Node.js. It is designed to be extremely fast and efficient, making it suitable for high-throughput applications. Pino offers a similar set of features to @sap-ux/logger but emphasizes performance and speed.
@sap-ux/logger
This is a simple logging library. It uses Winston underneath to do the heavy-lifting. The API is agnostic to any particular logging library. Don't depend on any Winston-specific implemenation as the underlying library may change in the future.
Quick Usage
Add the package @sap-ux/logger
to your project using your preferred package manager.
In Typescript/ES6, import the logger and instantiate it. By default the log level is info
and the logs are sent to the console.
import { ToolsLogger } from '@sap-ux/logger';
...
const logger = new ToolsLogger();
...
logger.info('This is an information message');
...
logger.warn('This is a warning');
...
logger.error('This is an error!');
logger.debug({a: 42, b: 'some value'});
Logger Options
Log Levels
Log levels are exposed an as enumeration.
The following levels are available, in decreasing order of severity:
- Error
- Warn
- Info
- Verbose
- Debug
- Silly
Transports
These are the targets of the log messages. You can have multiple targets for a single logger. The targets can be added and removed dynamically, if required.
The following transports are currently supported:
ConsoleTransport
This is used to write to the console
.
FileTransport
This is used to write to files. Logs are appended to files. Currently there's no support to rotate logs.
VSCodeTransport
This is used to write to an output channel in VS Code.
NullTransport
This is the equivalent of writing to /dev/null
. If an API needs a logger and you really don't want to capture logs, you can use NullTransport
to use completely ignore the logs.
Logger API
Logging methods
error()
, warn()
, info()
and debug()
methods can be used to either print log a string or an object.
Transport management
You can add()
, remove()
transport dynamically. transports()
returns a list of the current transports of the logger.
Examples
The unit test file can be used as an example to explore the logger's API: ./test/unit/wiston-logger/logger.test.ts
Please refer to the debugging section in the project root README for help debugging the test.
Not supported
The following options are not supported yet. There are no definite plans to add them in the future. They will be considered on a need-basis.
- Custom formatters
VSCodeTransport
logs timestamped linesFileTransport
logs in JSONConsoleTransport
logs colored and timestamped lines (piping will remove color encoding though)
- File rotation - the file transport appends to an existing file or creates a new one if missing