npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

winston-azure-transport

v3.0.5

Published

A winston transport for azure blobs

Downloads

117

Readme

winston-azure-transport

NPM version

A Windows Azure blob storage transport for winston. This is intended to replicate the "log to blob container functionality" found in Azure for node or iisnode applications, including log rollover and expiry in an easy to configure manner.

This borrows heavily from an original coffeescript version by Parsimotion, however it only requires a container SAS and works with winston 3.

Installation

  $ npm install winston
  $ npm install winston-azure-transport

Usage from iisnode

If node is being hosted in an Azure Windows app service (using iisnode), then configuration can be made largely automatic by using the Azure provided app settings. Firstly, to enable logging to storage from the Azure portal, navigate to your app service and configure "Application Logging (Blob)" from the "Diagnostic Logs" tab. Then add code to enable logging to your node application:

import winston from "winston";
import { getAzureBlobTransport } from "winston-azure-transport";

const logger = winston.createLogger({
    transports: getAzureBlobTransport()
});
logger.log("info", "Hello, world!");

getAzureBlobTransport will return null if it doesn't detect any configuration for azure blob logging (either from the environment provided by iisnode or from appsettings passed to it). This can be handled with code like:

function createLogger() {
    // Configure a console logger for winston
    const transports : TransportStream[] = [
        new winston.transports.Console({
            level: "info"
        })
    ];
    // Add an azure transport if configured
    const transport = getAzureBlobTransport();
    if (transport) {
        transports.push(transport);
    }
    // Create a winston logger
    const logger = winston.createLogger({
        transports
    });
    return logger;
}

You can also easily combine this with express using express-winston:

import express from "express";
import expressWinston from "express-winston";
import winston from "winston";
import { AzureBlobTransport, IAzureBlobTransportOptions } from "winston-azure-transport";

const app = express();

const logger = createLogger();

// Add express middleware
app.use(expressWinston.logger({
    winstonInstance,
    meta: true,
    expressFormat: true
}));

// Add more middleware
app.use(express.static("public"));
...
// etc

// Add an express error logger
app.use(expressWinston.errorLogger({
    winstonInstance
}));

This easily adds full logging to azure blob storage for actions and errors to iisnode applications.

General usage

For general usage AzureBlobTransport can be directly created with options passed to the constructor as in the example below.

import winston from "winston";
import { AzureBlobTransport } from "winston-azure-transport";

const logger = winston.createLogger({
    transports: [
        new AzureBlobTransport({
            containerUrl: "https://mystorage.blob.core.windows.net/errors?sv=2018-03-28&sr=c&sig=x&st=2019-01-01T00:00:00Z&se=2219-01-01T00:00:00Z&sp=rwdl",
            nameFormat: "my-app-logs/{yyyy}/{MM}/{dd}/{hh}/node.log",
            retention: 365
    ]
});
logger.log("info", "Hello, world!");

AzureBlobTransport accepts the following options:

containerUrl

The container url can be obtained from the Azure portal by navigating to your app service, configuring "Application Logging (Blob)" from the "Diagnostics Logs" tab, then viewing the "DIAGNOSTICS_AZUREBLOBCONTAINERSASURL" application setting on the "Application Settings" tab.

Alternatively you can use Azure Storage Explorer to generate the URL by choosing "Get Shared Access Signature" from the context menu of the storage container you wish to log to.

name

The name of the logger within winston (defaults to AzureBlobTransport).

nameFormat

The name format can be omitted and defaults to "{yyyy}/{MM}/{dd}/{hh}/node.log".

retention

The number of days to keep logs for. Log dates will be parsed according to the nameFormat option, and any logs older than the specified number of days will be deleted once per day.

trace

Setting trace: true in the options will enable debug messages from the logger. These go to the console.

getAzureBlobSettings

function getAzureBlobSettings(
    config?: Partial<IAzureBlobTransportOptions>,
    appSettings?: IAzureBlobTransportAppSettings
):  AzureBlobTransport | null;

This function detects settings from the node environment within in an azure app service. The environment variables DIAGNOSTICS_AZUREBLOBCONTAINERSASURL, DIAGNOSTICS_AZUREBLOBRETENTIONINDAYS are used for the containerUrl and retention settings respectively. The environment variable WEBSITE_SITE_NAME is used to provide a prefix to the nameFormat settings (in azure storage explorer, this appears as a folder with the same name as the website).

If the environment variables are not found then configuration is taken from appSettings (described below), if provided. If neither method provides an SAS storage url then the function returns null.

The config argument allows passing on any additional options to the AzureBlobTransport constructor. The appSettings argument allows passing a json configuration file on as follows:

const appSettings = require("appsettings.json");
const transport = getAzureBlobSettings({}, appSettings);

With appsettings.json being something like:

{
    "azureBlobContainerSasUrl": "https://mystorage.blob.core.windows.net/errors?sv=2018-03-28&sr=c&sig=x&st=2019-01-01T00:00:00Z&se=2219-01-01T00:00:00Z&sp=rwdl",
    "azureBlobContainerSasFormat": "my-folder/{yyyy}/{MM}/{dd}/{hh}/node.log",
    "azureBlobRetentionInDays": 365
}