New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fm-log

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fm-log - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

.idea/.name

141

lib/log.js
"use strict";
var colors = require( "colors" );
var Type = require( "type-of-is" );

@@ -12,2 +13,41 @@ /**

/**
* The last logging function that was used.
* @type {Function}
*/
var lastLogger;
/**
* The last message that was logged.
* @type {String}
*/
var lastMessage;
/**
* How often was the last message repeated.
* @type {number}
*/
var lastMessageRepeated = 0;
/**
* Determines if a given message should be logged. The purpose is to avoid logging the same message continously.
* @param {String} message The message being logged.
* @returns {boolean} true if the message should be logged; false otherwise.
*/
var shouldLog = function( message ) {
if( message == lastMessage ) {
++lastMessageRepeated;
return false;
} else if( lastMessage && 0 < lastMessageRepeated ) {
lastMessage = undefined;
lastLogger( "Last message repeated " + lastMessageRepeated + " times." );
lastMessageRepeated = 0;
} else {
lastMessage = message;
}
return true;
};
/**
* The main logger class.

@@ -33,6 +73,11 @@ */

*/
var constructPrefix = function( prefix ) {
return (prefix) ? "(" + pad( prefix, moduleMaxLength, " " ) + ")" : pad( "", moduleMaxLength + 2, " " );
}
var constructPrefix = function( prefix, multiline ) {
// If there is no prefix and nothing else has a prefix, then the .module construction scheme is never used.
// In this case, don't pad anything.
if( !prefix && !moduleMaxLength ) return "";
// If we have a prefix, pad it to the max prefix length, otherwise, leave off the parenthesis and just use spaces.
return (prefix && !multiline) ? "(" + pad( prefix, moduleMaxLength, " " ) + ")" : pad( "", moduleMaxLength + 2, " " );
};
/**

@@ -44,5 +89,5 @@ * Prefix a message with supplied prefix.

*/
var prefixMessage = function( prefix, message ) {
var prefixMessage = function( prefix, message, multiline ) {
// Pad the prefix so that all logged messages line up
prefix = constructPrefix( prefix );
prefix = constructPrefix( prefix, multiline );
return (prefix) ? (prefix + " " + message ) : message;

@@ -53,3 +98,3 @@ };

* Pad a given string with another string(usually a single character), to a certain length.
* @param {String} padWhat The string that should be padded.
* @param {String|Number} padWhat The string that should be padded.
* @param {Number} [length=2] How long the resulting string should be.

@@ -66,2 +111,19 @@ * @param {String} [padWith="0"] What character should be used for the padding.

var generateLogStack = function( level, prefix, subject, colorizer ) {
var lines = subject.split( "\n" );
// Most common case, a single line.
if( lines.count == 1 ) {
return colorizer( level + " " + prefixMessage( prefix, subject ) );
}
// Multiple lines, prepare them all nice like.
for( var lineIndex = 0, lineCount = lines.length; lineIndex < lineCount; ++lineIndex ) {
lines[lineIndex] = colorizer( level + " " + prefixMessage( prefix, lines[ lineIndex ], lineIndex > 0 ) );
// Replace the level prefix with whitespace for further lines
if( 0 == lineIndex ) {
level = pad( "", level.length, " " );
}
}
return 1 < lines.length ? lines : lines[ 0 ];
};
/**

@@ -72,5 +134,4 @@ * Log a debug message.

Logger.prototype.debug = function( message ) {
var toLog = "[DEBUG ] " + prefixMessage( this.prefix, message );
this.log( toLog.grey );
}
preLog( "[DEBUG ]", this.prefix, colors.grey, this.debug.bind( this ), message );
};
/**

@@ -81,5 +142,4 @@ * Log an informational message.

Logger.prototype.info = function( message ) {
var toLog = "[INFO ] " + prefixMessage( this.prefix, message );
this.log( toLog.cyan );
}
preLog( "[INFO ]", this.prefix, colors.cyan, this.info.bind( this ), message );
};
/**

@@ -90,5 +150,4 @@ * Log a notice.

Logger.prototype.notice = function( message ) {
var toLog = "[NOTICE] " + prefixMessage( this.prefix, message );
this.log( toLog.green );
}
preLog( "[NOTICE]", this.prefix, colors.green, this.notice.bind( this ), message );
};
/**

@@ -99,5 +158,4 @@ * Log a warning.

Logger.prototype.warn = function( message ) {
var toLog = "[WARN ] " + prefixMessage( this.prefix, message );
this.log( toLog.yellow );
}
preLog( "[WARN ]", this.prefix, colors.yellow, this.warn.bind( this ), message );
};
/**

@@ -108,5 +166,4 @@ * Log an error.

Logger.prototype.error = function( message ) {
var toLog = "[ERROR ] " + prefixMessage( this.prefix, message );
this.log( toLog.red );
}
preLog( "[ERROR ]", this.prefix, colors.red, this.error.bind( this ), message );
};
/**

@@ -117,5 +174,5 @@ * Log a critical event.

Logger.prototype.critical = function( message ) {
var toLog = "[CRITIC] " + prefixMessage( this.prefix, message );
this.log( toLog.red.bold );
}
preLog( "[CRITIC]", this.prefix, function( str ) {return colors.bold( colors.red( str ) ); }, this.critical.bind( this ), message );
};
// Aliases

@@ -126,2 +183,14 @@ Logger.prototype.verbose = Logger.prototype.debug;

var preLog = function( level, prefix, colorizer, more, message ) {
if( Type.is( message, Error ) ) {
/** @type {Error} */
var error = message;
more( error.stack );
return;
}
var toLog = generateLogStack( level, prefix, message, colorizer );
log( toLog, more );
}
/**

@@ -131,7 +200,17 @@ * Log a message.

*/
Logger.prototype.log = function( message ) {
var log = function( message, more ) {
if( !shouldLog( message ) ) {
return;
}
lastLogger = more;
var time = new Date();
var timestamp = this.formatDate( time );
console.log( timestamp + " " + message );
}
var timestamp = formatDate( time );
if( Type.is( message, Array ) ) {
for( var messageIndex = 0, messageCount = message.length; messageIndex < messageCount; ++messageIndex ) {
console.log( timestamp + " " + message[ messageIndex ] );
}
} else {
console.log( timestamp + " " + message );
}
};

@@ -143,6 +222,6 @@ /**

*/
Logger.prototype.formatDate = function( date ) {
var formatDate = function( date ) {
return date.getFullYear() + "-" + pad( date.getMonth() + 1 ) + "-" + pad( date.getDate() ) + " " +
pad( date.getHours() ) + ":" + pad( date.getMinutes() ) + ":" + pad( date.getSeconds() ) + "." + pad( date.getMilliseconds(), 3 );
}
};

@@ -159,3 +238,3 @@ /**

return new Logger( moduleName );
}
};
return Logger;

@@ -162,0 +241,0 @@ }());

{
"name": "fm-log",
"version": "0.0.1",
"version": "0.1.0",
"description": "Console logging facility for Node",

@@ -24,5 +24,12 @@ "main": "lib/log.js",

"homepage": "https://github.com/hartwig-at/fm-log",
"devDependencies": {
"grunt": ">= 0.4.4",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-watch": "~0.6.1",
"should": "^3.3.1"
},
"dependencies": {
"colors": "^0.6.2"
"colors": "^0.6.2",
"type-of-is": "^3.2.0"
}
}

@@ -6,4 +6,20 @@ fm-log

-----
Straight-forward logging module. Lines up everything in nice columns. Uses colors. Sends everything straight to `console.log`, no events, no `nextTick()`.
Straight-forward logging module.
- lines up everything in nice columns
- uses colors
- sends everything straight to `console.log`, no events, no `nextTick()`
- condenses repeated messages like:
2014-04-24 17:17:00.138 [DEBUG ] ( api-mobile.js) Cleaning up messages...
2014-04-24 17:34:32.715 [DEBUG ] ( api-mobile.js) Last message repeated 17 times.
2014-04-24 17:34:32.715 [INFO ] ( api-person.js) API-REQUEST: List Person...
- displays stack traces for logged Error instances and other multi-line content like:
![](img/example2.png)
Example
-------
var log = require( "fm-log" ).module( path.basename( __filename ) );

@@ -22,3 +38,3 @@ log.info( "Initializing application..." );

![](example.png)
![](img/example.png)

@@ -25,0 +41,0 @@ How?

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