What is @azure/logger?
@azure/logger is a logging utility provided by Microsoft Azure that allows developers to create and manage logs for their applications. It is designed to be lightweight and easy to use, making it suitable for both small and large-scale applications. The package provides various functionalities to control the logging level, format, and output destination.
What are @azure/logger's main functionalities?
Basic Logging
This feature allows you to create a logger instance and log messages at different levels such as info, warning, and error.
const { createClientLogger } = require('@azure/logger');
const logger = createClientLogger('my-namespace');
logger.info('This is an info message');
logger.warning('This is a warning message');
logger.error('This is an error message');
Setting Log Level
This feature allows you to set the log level, so only messages at that level or higher will be logged. In this example, only warning and error messages will be logged.
const { setLogLevel } = require('@azure/logger');
setLogLevel('warning');
const { createClientLogger } = require('@azure/logger');
const logger = createClientLogger('my-namespace');
logger.info('This info message will not be logged');
logger.warning('This warning message will be logged');
Custom Loggers
This feature allows you to create custom loggers with different namespaces, which can help in organizing and filtering logs based on different parts of your application.
const { createClientLogger } = require('@azure/logger');
const customLogger = createClientLogger('custom-namespace');
customLogger.info('This is a custom info message');
Other packages similar to @azure/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 can be extended with custom transports and formats. Compared to @azure/logger, Winston offers more flexibility and a broader range of features, but it may be more complex to set up.
bunyan
Bunyan is a simple and fast JSON logging library for Node.js. It is designed to produce logs in a structured JSON format, making it easy to parse and analyze. Bunyan also supports log levels and multiple output streams. Compared to @azure/logger, Bunyan focuses on JSON logging and may be more suitable for applications that require structured log data.
pino
Pino is a fast and low-overhead logging library for Node.js. It is designed for high-performance logging and produces logs in a JSON format. Pino supports log levels and can be integrated with various transports and log management systems. Compared to @azure/logger, Pino is optimized for performance and may be a better choice for applications with high logging throughput.
Azure Logger library for JS
The @azure/logger
package can be used to enable logging in the Azure SDKs for JavaScript.
Logging can be enabled for the Azure SDK in the following ways:
- Setting the AZURE_LOG_LEVEL environment variable
- Calling setLogLevel imported from "@azure/logger"
- Calling enable() on specific loggers
- Using the
DEBUG
environment variable.
Note that AZURE_LOG_LEVEL, if set, takes precedence over DEBUG. Only use DEBUG without specifying AZURE_LOG_LEVEL or calling setLogLevel.
Getting started
Installation
Install this library using npm as follows
npm install @azure/logger
Key Concepts
The @azure/logger
package supports the following log levels
specified in order of most verbose to least verbose:
When setting a log level, either programmitcally or via the AZURE_LOG_LEVEL
environment variable,
any logs that are written using a log level equal to or less than the one you choose
will be emitted.
For example, setting the log level to warning
will cause all logs that have the log
level warning
or error
to be emitted.
Examples
Example 1 - basic usage
const { EventHubClient } = require('@azure/event-hubs');
const logger = require('@azure/logger');
logger.setLogLevel('info');
const client = new EventHubClient();
client.getPartitionIds()
.then(ids => { })
.catch(e => { });
});
Example 2 - redirect log ouput
const { AzureLogger, setLogLevel } = require("@azure/logger");
setLogLevel("verbose");
AzureLogger.log = (...args) => {
console.log(...args);
};
Using AzureLogger
, it is possible to redirect the logging output from the Azure SDKs by
overriding the AzureLogger.log
method. This may be useful if you want to redirect logs to
a location other than stderr.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor
License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your
contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and
decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot.
You will only need to do this once across all repos using our CLA.
If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.
This project has adopted the Microsoft Open Source Code of Conduct.
For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional
questions or comments.