
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
winston-azure-application-insights
Advanced tools
Azure Application Insights transport for Winston
An Azure Application Insights transport for Winston logging library.
This transport is designed to make it easy to obtain a reference to a standard logging library that broadcasts to Application Insights.
Your logging interface can remain familiar to standard (logger.info
, logger.error
etc) without intertwining any Azure-specific implementation detail.
This library intends to be compatible with applicationinsights
1.0
and Winston 3.x
. If you are using older versions of these libraries, see the 1.x
releases.
It works best with applicationinsights@~1.0.6
due to improved trace property handling.
You'll need the following packages as peer dependencies; install and update them in your requiring project:
winston
logger packageapplicationinsights
librarynpm install winston-azure-application-insights
They aren't required for you, in case you want to run a specific version.
This library uses ES6 which should be compatible with NodeJS 6 through to 10.
Continuous integration tests are run against the NodeJS LTS versions.
See demo.js
for a small example.
Instrumentation key
Note: an instrumentation key is required before any data can be sent. Please see the "Getting an Application Insights Instrumentation Key" for more information.
The instrumentation key can be supplied in 4 ways:
const { AzureApplicationInsightsLogger } = require('winston-azure-application-insights');
// Create an app insights client with the given key
winston.add(new AzureApplicationInsightsLogger({
key: "<YOUR_INSTRUMENTATION_KEY_HERE>"
}));
const appInsights = require("applicationinsights");
const { AzureApplicationInsightsLogger } = require('winston-azure-application-insights');
appInsights.setup("<YOUR_INSTRUMENTATION_KEY_HERE>").start();
// Use an existing app insights SDK instance
winston.add(new AzureApplicationInsightsLogger({
insights: appInsights
}));
const appInsights = require("applicationinsights");
const { AzureApplicationInsightsLogger } = require('winston-azure-application-insights');
appInsights.setup("<YOUR_INSTRUMENTATION_KEY_HERE>").start();
// Create a new app insights client with another key
winston.add(new AzureApplicationInsightsLogger({
client: appInsights.getClient("<ANOTHER_INSTRUMENTATION_KEY_HERE>")
}));
APPINSIGHTS_INSTRUMENTATIONKEY
environment variable (supported by the Application Insights SDK)I get an error when using this transport
If you receive the error:
"Instrumentation key not found, pass the key in the config to this method or set the key in the environment variable APPINSIGHTS_INSTRUMENTATIONKEY before starting the server"
Then you didn't specify a suitable instrumentation key. See the section above.
I get an error "Zones already loaded"
This may be because your environment has already (maybe implicitly) loaded applicationinsights and called .setup()
.
This happens if you are running an Azure Function App and have APPINSIGHTS_INSTRUMENTATIONKEY
set.
The best solution to this is to load applicationinsights
and pass in appInsights.defaultClient
using the client
option as per example 3.
I'm seeing multiple traces with similar/identical messages
applicationinsights
deeply integrates into the console
transports, and winston
itself (via diagnostic-channel
).
If you are integrating this transport, it's recommended to disable diagnostic-channel
and console auto collection:
To control diagnostic-channel
, follow the guide in the main repository.
Note that better control is afforded in versions from 1.0.5
.
It is recommended to use only this transport where your application is running in production mode and needs to stream data to Application Insights. In all other scenarios such as local debug and test suites, the console transport (or similar) should suffice. This is to avoid polluting instances/unnecessary cost.
Despite this notice, to specifically disable console transport collection, use .setAutoCollectConsole(false)
:
const appInsights = require('applicationinsights');
appInsights.setup(process.env.APPINSIGHTS_INSTRUMENTATIONKEY)
.setAutoCollectConsole(false);
info
)true
).SDK integration options (required):
Ony one of the above option parameters will be used, in this order: client, insights, key.
APPINSIGHTS_INSTRUMENTATIONKEY
Supported log levels are:
Winston Level | App Insights level |
---|---|
emerg | critical (4) |
alert | critical (4) |
crit | critical (4) |
error | error (3) |
warning | warning (2) |
warn | warning (2) |
notice | informational (1) |
info | informational (1) |
verbose | verbose (0) |
debug | verbose (0) |
silly | verbose (0) |
All other possible levels, or custom levels, will default to info
The Application Insights "exceptions" table allows you to see more detailed error information including the stack trace.
Therefore for all log events at severity level error or above, an exception is logged if the library detects that an
Error object has been passed.
The log event will still generate a trace with the correct severity level regardless of this setting, but please note
that any Error object will have its stack
property omitted when sent to trackTrace
.
All other properties are included.
This allows you to see clearly Azure Application Insights instead of having to access trace information manually and set up alerts based on the related metrics.
How it works with sendErrorsAsExceptions: true
:
logger.error('error message');
creates a trace with severity level 3; no exception is trackedlogger.error(new Error('error message'));
creates a trace with severity level 3, and an exception with the Error object as argumentlogger.error('error message', new Error('error message'));
creates a trace with severity level 3, and an exception with the Error object as argumentlogger.error(new Error('error message'), logContext);
creates a trace and exception and logContext is set to the customDimensions (properties) track* fieldlogger.info(new Error('error message'));
creates a trace with severity level 1; no exception is trackedIf you do not wish to track exceptions, you can set the option sendErrorsAsExceptions: false
when configuring the transport.
FAQs
Azure Application Insights transport for Winston
The npm package winston-azure-application-insights receives a total of 14,190 weekly downloads. As such, winston-azure-application-insights popularity was classified as popular.
We found that winston-azure-application-insights demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.