'use strict';

let winston = require('winston');

winston.setLevels({
    audit: 0,
    error: 1,
    warn: 2,
    info: 3,
    debug: 4
});
winston.addColors({
    audit: 'cyan',
    error: 'red',
    warn: 'yellow',
    info: 'green',
    debug: 'grey'
});

winston.clear();
if (process.env.NODE_ENV === 'test') {
    winston.add(winston.transports.File, {
        name: 'log',
        level: 'debug',
        filename: 'tests.log',
        json: false,
        colorize: true,
        prettyPrint: true
    });
} else if (process.env.NODE_ENV === 'production') {
    winston.add(winston.transports.Console, {
        name: 'log',
        level: 'info',
        prettyPrint: false,
        colorize: false,
        silent: false,
        timestamp: true,
        json: true,
        stringify: true,
        handleExceptions: true
    });
} else {
    winston.add(winston.transports.Console, {
        name: 'log',
        level: 'info',
        // prettyPrint: true,
        colorize: true,
        silent: false,
        timestamp: true,
        handleExceptions: true,
        json: false,
        humanReadableUnhandledException: true
    });
}

const appInsights = require('./azure-appinsights');
if (appInsights) {
    const aiLogger = require('winston-azure-application-insights');
    winston.info('Activating application insights logger');
    winston.add(aiLogger.AzureApplicationInsightsLogger, {
        insights: appInsights,
        level: 'info',
        silent: false,
        treatErrorsAsExceptions: true
    });
}

module.exports = winston;

@@ -1526,16 +1526,778 @@
     %7D);%0A
+    aiLogger.rewriters.push(function(level, msg, meta) %7B%0A        return flattenMeta(meta);%0A    %7D);%0A%7D%0A%0Afunction flattenObject(source, target, prefix) %7B%0A    Object.keys(source).forEach(function(key) %7B%0A        let sourceVal = source%5Bkey%5D;%0A        let fullKey = prefix + '_' + key;%0A        if (sourceVal && typeof sourceVal === %22object%22) %7B%0A            flattenObject(sourceVal, target, fullKey)%0A        %7D else %7B%0A            target%5BfullKey%5D = sourceVal;%0A        %7D%0A    %7D);%0A%7D%0A%0Afunction flattenMeta(meta) %7B%0A    let flat = %7B%7D;%0A    Object.keys(meta).forEach(function(key) %7B%0A        let val = meta%5Bkey%5D;%0A        if (val && typeof val === %22object%22) %7B%0A            flattenObject(val, flat, key);%0A        %7D else %7B%0A            flat%5Bkey%5D = val;%0A        %7D%0A    %7D);%0A    return flat;%0A
 %7D%0A%0Amodul

Rewrite azure log message via flattening