Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cli-logger

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cli-logger - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

log/.npmignore

173

index.js
var events = require('events');
var fs = require('fs');
var os = require('os');

@@ -6,4 +7,9 @@ var path = require('path'),

var util = require('util');
var Writable = require('stream').Writable;
var merge = require('cli-util').merge;
var RAW = 'raw';
var STREAM = 'stream';
var FILE = 'file';
var levels = {

@@ -20,3 +26,3 @@ trace: 10,

name: basename(process.argv[1]),
json: true
json: false
}

@@ -31,3 +37,13 @@

conf = conf || {};
this.conf = merge(conf, merge(defaults, {}));
function filter(t, k, v) {
if(k !== 'streams') {
t[k] = v;
}
}
this.conf = merge(conf, merge(defaults, {}), filter);
this.conf.streams = conf.streams || {
stream: process.stdout,
level: levels.info
}
this.streams = this.initialize();
this.pid = process.pid;

@@ -40,2 +56,49 @@ this.hostname = os.hostname();

/**
* Initialize the output streams.
*
* @api private
*/
Logger.prototype.initialize = function() {
var streams = [], scope = this;
var source = this.conf.streams;
function append(stream, level, name) {
stream.on('error', function(e) {
scope.emit('error', e, stream);
})
streams.push({stream: stream, level: level || levels.info, name: name})
}
function wrap(source) {
var stream = source.stream;
if(source.path) {
var opts = {
flags: source.flags || 'a',
mode: source.mode,
encoding: source.encoding
}
try {
source.stream = fs.createWriteStream(source.path, opts);
}catch(e) {
scope.emit('error', e);
}
}
if(source.stream && !(source.stream instanceof Writable)
&& source.stream !== process.stdout
&& source.stream !== process.stderr) {
throw new Error('Invalid stream specified');
}
append(source.stream, source.level, source.name);
}
if(source && typeof(source) == 'object' && !Array.isArray(source)) {
wrap(source);
}else if(Array.isArray(source)) {
source.forEach(function(source) {
wrap(source);
})
}else{
throw new Error('Invalid streams configuration');
}
return streams;
}
/**
* Retrieve a log record.

@@ -50,20 +113,75 @@ *

Logger.prototype.getLogRecord = function(level, message) {
var parameters = [].slice.call(arguments, 2);
var parameters = [].slice.call(arguments, 2), args, z;
var err = (message instanceof Error) ? message : null;
var obj = (!err && message && typeof(message) == 'object') ? message : null;
if(parameters.length) {
parameters.unshift(message);
if(!err && !obj) {
parameters.unshift(message);
}
message = util.format.apply(util, parameters);
}
if(err) {
if(arguments.length == 2) {
message = err.message;
}//else{
//args = [].slice.call(arguments, 2);
//message = util.format.apply(util, args);
//}
}
var record = message;
if(this.conf.json) {
record = {
pid: this.pid,
hostname: this.hostname,
name: this.conf.name,
msg: message
};
record = {};
if(obj) {
for(z in obj) {
record[z] = obj[z];
}
if(arguments.length == 2) {
message = '';
}
}
record.pid = this.pid;
record.hostname = this.hostname;
record.name = this.conf.name;
record.msg = message;
record.level = level;
record.time = new Date().toISOString();
if(err) {
record.err = {
message: err.message,
name: err.name,
stack: err.stack
}
}
}
console.log('logging message...%j', record);
return record;
}
/**
* Write the log record to stream(s) or dispatch
* the write event if there are listeners for the write
* event.
*
* @api private
*
* @param level The log level.
* @param record The log record.
*/
Logger.prototype.write = function(level, record) {
var i, target, listeners = this.listeners('write');
for(i = 0;i < this.streams.length;i++) {
target = this.streams[i];
if(!listeners.length && this.conf.json && target.type !== RAW) {
record = JSON.stringify(record);
}
if(level >= target.level) {
if(listeners.length) {
this.emit('write', record, target.stream);
}else{
target.stream.write(record + '\n');
}
}
}
}
/**
* Log a message.

@@ -79,7 +197,30 @@ *

if(!message) return false;
var record = this.getLogRecord.apply(this, arguments);
//console.log('logging message...%s', message);
this.write(level, this.getLogRecord.apply(this, arguments));
}
/**
* Log a trace message.
*
* @param message The log message.
* @param ... The message replacement parameters.
*/
Logger.prototype.trace = function() {
var args = [].slice.call(arguments, 0);
args.unshift(levels.trace);
this.log.apply(this, args);
}
/**
* Log a debug message.
*
* @param message The log message.
* @param ... The message replacement parameters.
*/
Logger.prototype.debug = function() {
var args = [].slice.call(arguments, 0);
args.unshift(levels.debug);
this.log.apply(this, args);
}
/**
* Log an info message.

@@ -102,1 +243,7 @@ *

module.exports.Logger = Logger;
module.exports.TRACE = levels.trace;
module.exports.DEBUG = levels.debug;
module.exports.INFO = levels.info;
module.exports.WARN = levels.warn;
module.exports.ERROR = levels.error;
module.exports.FATAL = levels.fatal;

2

package.json
{
"name": "cli-logger",
"version": "0.0.1",
"version": "0.0.2",
"description": "Logger implementation for command line interfaces",

@@ -5,0 +5,0 @@ "author": "muji <noop@xpm.io>",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc