How to use the logform.format function in logform

To help you get started, we’ve selected a few logform examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hyperledger / caliper / packages / caliper-core / lib / common / utils / log-formats.js View on Github external
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const colors = require('colors/safe');
const { format } = require('logform');
const { LEVEL } = require('triple-beam');

const attributeRegex = /%attribute%/gi;

const colorizeExtra = format((info, opts) => {
    // The immutable level string of the message (the normal property could be mutated already)
    let lev = info[LEVEL];
    // colors enables multiple styles separated by spaces
    let colorStyles = opts.colors[lev].split(' ');

    for (let key of Object.keys(info)) {
        if (info[key] !== undefined && (opts.all || opts[key])) {
            // surround the value with the style codes one by one
            for (let style of colorStyles) {
                try {
                    info[key] = colors[style](info[key]);
                } catch (e) {
                    // silent fail, can't log here
                }
            }
        }
github hyperledger / caliper / packages / caliper-core / lib / common / utils / log-formats.js View on Github external
if (info[key] !== undefined && (opts.all || opts[key])) {
            // surround the value with the style codes one by one
            for (let style of colorStyles) {
                try {
                    info[key] = colors[style](info[key]);
                } catch (e) {
                    // silent fail, can't log here
                }
            }
        }
    }

    return info;
});

const padLevelExtra = format(info => {

    let padding = ' '.repeat(Math.max(5 - info[LEVEL].length, 0));
    info.level = `${info.level}${padding}`;
    return info;
});

const attributeFormat = format((info, opts) => {
    for (let key of Object.keys(info)) {
        if (typeof opts[key] === 'string') {
            if (typeof info[key] !== 'string') {
                info[key] = JSON.stringify(info[key]);
            }

            info[key] = opts[key].replace(attributeRegex, info[key]);
        }
    }
github kubernetes-simulator / simulator / tools / scenario-tools / lib / logger.js View on Github external
const { inspect } = require('util')
const { MESSAGE, SPLAT } = require('triple-beam')
const winston = require('winston')
const { format } = require('logform')

const formatLogMessage = format((info, opts) => {
  const depth = opts.depth || null
  if (info[SPLAT]) {
    for (const splat of info[SPLAT]) {
      info.message += '\n' + inspect(splat, false, depth, opts.colorize)
    }
  }

  info[MESSAGE] = `${info.level}:${info.message}`

  return info
})

// Creates a new logger with the supplied options.
// Options are:
// - colorize - whethers to colourise the output
// - level - the logging level
github hyperledger / caliper / packages / caliper-core / lib / common / utils / log-formats.js View on Github external
}
            }
        }
    }

    return info;
});

const padLevelExtra = format(info => {

    let padding = ' '.repeat(Math.max(5 - info[LEVEL].length, 0));
    info.level = `${info.level}${padding}`;
    return info;
});

const attributeFormat = format((info, opts) => {
    for (let key of Object.keys(info)) {
        if (typeof opts[key] === 'string') {
            if (typeof info[key] !== 'string') {
                info[key] = JSON.stringify(info[key]);
            }

            info[key] = opts[key].replace(attributeRegex, info[key]);
        }
    }

    return info;
});

module.exports.ColorizerExtra = colorizeExtra;
module.exports.PadLevelExtra = padLevelExtra;
module.exports.AttributeFormat = attributeFormat;

logform

An mutable object-based log format designed for chaining & objectMode streams.

MIT
Latest version published 4 months ago

Package Health Score

89 / 100
Full package analysis