Skip to content

Commit

Permalink
Merge pull request #15 from jonnyreeves/develop
Browse files Browse the repository at this point in the history
v1.0.0 Release
  • Loading branch information
jonnyreeves committed May 7, 2015
2 parents 760cf7b + feb34fb commit a69d7f7
Show file tree
Hide file tree
Showing 16 changed files with 10,373 additions and 7,561 deletions.
12 changes: 12 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"curly": true,
"eqeqeq": true,
"undef": true,
"browser": true,
"strict": true,
"globals": {
"console": true,
"define": true,
"module": true
}
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.0 (18th April, 2015)
- Introduce timing operations (#time and #timeEnd)
- Update gulp dependencies.

## 0.9.14 (4th September, 2014)
- Fix for IE7 (#10, @james-west)

Expand Down
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,26 @@ Okay, let's get serious, logging is not for kids, it's for adults with serious s

// As it's the same instance being returned each time, you don't have to store a reference:
Logger.get('ModuleA').warn('FizzWozz combombulated!");

Note that `Logger.setLevel()` will also change the current log filter level for all named logger instances; so typically you would configure your logger levels like so:

// Create a couple of named loggers (typically in their own AMD file)
var loggerA = Logger.get('LoggerA');
var loggerB = Logger.get('LoggerB');

// Configure log levels.
Logger.setLevel(Logger.WARN); // Global logging level.
Logger.get('LoggerB').setLevel(Logger.DEBUG); // Enable debug logging for LoggerB
// Create a couple of named loggers (typically in their own module)
var loggerA = Logger.get('LoggerA');
var loggerB = Logger.get('LoggerB');

// Configure log levels.
Logger.setLevel(Logger.WARN); // Global logging level.
Logger.get('LoggerB').setLevel(Logger.DEBUG); // Enable debug logging for LoggerB

## Profiling
Sometimes its good to know what's taking so damn long; you can use `Logger.time()` and `Logger.timeEnd()` to keep tabs on things, the default log handler implementation delegates to the equivalent console methods if they exist, or write to `console.log` if they don't.

// Start timing something
Logger.time('self destruct sequence');

// ... Some time passes ...

// Stop timing something.
Logger.timeEnd('self destruct sequence'); // logs: 'self destruct sequence: 1022ms'.

Note that `time` and `timeEnd` methods are also provided to named Logger instances.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-logger",
"version": "0.9.14",
"version": "1.0.0",
"main": "src/logger.js",
"ignore": [
"**/.*",
Expand Down
9 changes: 4 additions & 5 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var packageJSON = require('./package.json');
var gulp = require('gulp');
var gutil = require('gulp-util');
var uglify = require('gulp-uglify');
var replace = require('gulp-replace');
var rename = require('gulp-rename');
Expand Down Expand Up @@ -28,7 +27,7 @@ gulp.task('bower_version', function () {
gulp.task('version', [ 'src_version', 'bower_version' ]);

gulp.task('lint', [ 'version' ], function () {
return gulp.src(srcFile)
return gulp.src([ srcFile, 'test-src/*.js' ])
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(jshint.reporter('fail'));
Expand All @@ -52,9 +51,9 @@ gulp.task('release', [ 'push_tag', 'publish_npm' ]);

gulp.task('push_tag', function (done) {
git.tag(version, 'v' + version);

spawn('git', [ 'push', '--tags' ], { stdio: 'inherit' })
.on('close', done);
git.push('origin', 'master', {args: " --tags"}, function (err) {
done(err);
});
});

gulp.task('publish_npm', function (done) {
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-logger",
"version": "0.9.14",
"version": "1.0.0",
"description": "Lightweight, unobtrusive, configurable JavaScript logger",
"author": "Jonny Reeves (http://jonnyreeves.co.uk)",
"homepage": "http://github.com/jonnyreeves/js-logger",
Expand All @@ -22,14 +22,14 @@
"test": "gulp test lint"
},
"devDependencies": {
"gulp": "~3.5.2",
"gulp": "~3.8.11",
"gulp-util": "~2.2.14",
"gulp-uglify": "~0.2.1",
"gulp-replace": "~0.2.0",
"gulp-rename": "~1.0.0",
"gulp-qunit": "~0.3.3",
"gulp-jshint": "~1.4.0",
"gulp-size": "~0.3.1",
"gulp-git": "~0.4.3"
"gulp-uglify": "~1.2.0",
"gulp-replace": "~0.5.3",
"gulp-rename": "~1.2.0",
"gulp-qunit": "~1.2.1",
"gulp-jshint": "~1.10.0",
"gulp-size": "~1.2.1",
"gulp-git": "~1.2.0"
}
}
71 changes: 56 additions & 15 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@
* Jonny Reeves, http://jonnyreeves.co.uk/
* js-logger may be freely distributed under the MIT license.
*/

/*jshint sub:true*/
/*global console:true,define:true, module:true*/
(function (global) {
"use strict";

// Top level module for the global, static logger instance.
var Logger = { };

// For those that are at home that are keeping score.
Logger.VERSION = "0.9.14";
Logger.VERSION = "1.0.0";

// Function which handles all incoming log messages.
var logHandler;

// Map of ContextualLogger instances by name; used by Logger.get() to return the same named instance.
var contextualLoggersByNameMap = {};

// Polyfill for ES5's Function.bind.
var bind = function(scope, func) {
return function() {
Expand Down Expand Up @@ -49,6 +46,7 @@
// Predefined logging levels.
Logger.DEBUG = defineLogLevel(1, 'DEBUG');
Logger.INFO = defineLogLevel(2, 'INFO');
Logger.TIME = defineLogLevel(3, 'TIME');
Logger.WARN = defineLogLevel(4, 'WARN');
Logger.ERROR = defineLogLevel(8, 'ERROR');
Logger.OFF = defineLogLevel(99, 'OFF');
Expand Down Expand Up @@ -92,6 +90,18 @@
this.invoke(Logger.ERROR, arguments);
},

time: function (label) {
if (typeof label === 'string' && label.length > 0) {
this.invoke(Logger.TIME, [ label, 'start' ]);
}
},

timeEnd: function (label) {
if (typeof label === 'string' && label.length > 0) {
this.invoke(Logger.TIME, [ label, 'end' ]);
}
},

// Invokes the logger callback if it's not being filtered.
invoke: function (level, msgArgs) {
if (logHandler && this.enabledFor(level)) {
Expand All @@ -110,6 +120,8 @@

L.enabledFor = bind(globalLogger, globalLogger.enabledFor);
L.debug = bind(globalLogger, globalLogger.debug);
L.time = bind(globalLogger, globalLogger.time);
L.timeEnd = bind(globalLogger, globalLogger.timeEnd);
L.info = bind(globalLogger, globalLogger.info);
L.warn = bind(globalLogger, globalLogger.warn);
L.error = bind(globalLogger, globalLogger.error);
Expand Down Expand Up @@ -154,6 +166,15 @@
return;
}

// Map of timestamps by timer labels used to track `#time` and `#timeEnd()` invocations in environments
// that don't offer a native console method.
var timerStartTimeByLabelMap = {};

// Support for IE8+ (and other, slightly more sane environments)
var invokeConsoleMethod = function (hdlr, messages) {
Function.prototype.apply.call(hdlr, console, messages);
};

Logger.setLevel(defaultLevel || Logger.DEBUG);
Logger.setHandler(function(messages, context) {
var hdlr = console.log;
Expand All @@ -163,17 +184,37 @@
messages[0] = "[" + context.name + "] " + messages[0];
}

// Delegate through to custom warn/error loggers if present on the console.
if (context.level === Logger.WARN && console.warn) {
hdlr = console.warn;
} else if (context.level === Logger.ERROR && console.error) {
hdlr = console.error;
} else if (context.level === Logger.INFO && console.info) {
hdlr = console.info;
if (context.level === Logger.TIME) {
if (messages[1] === 'start') {
if (console.time) {
console.time(messages[0]);
}
else {
timerStartTimeByLabelMap[messages[0]] = new Date().getTime();
}
}
else {
if (console.timeEnd) {
console.timeEnd(messages[0]);
}
else {
invokeConsoleMethod(hdlr, [ messages[0] + ': ' +
(new Date().getTime() - timerStartTimeByLabelMap[messages[0]]) + 'ms' ]);
}
}
}
else {
// Delegate through to custom warn/error loggers if present on the console.
if (context.level === Logger.WARN && console.warn) {
hdlr = console.warn;
} else if (context.level === Logger.ERROR && console.error) {
hdlr = console.error;
} else if (context.level === Logger.INFO && console.info) {
hdlr = console.info;
}

// Support for IE8+ (and other, slightly more sane environments)
Function.prototype.apply.call(hdlr, console, messages);
invokeConsoleMethod(hdlr, messages);
}
});
};

Expand All @@ -193,5 +234,5 @@
};

global.Logger = Logger;
}
}
}(this));
2 changes: 1 addition & 1 deletion src/logger.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test-src/.jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../.jshintrc",
"globals": {
"Logger": true,
"QUnit": true,
"sinon": true
}
}
9 changes: 6 additions & 3 deletions test-src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
<html>
<head>
<!-- Qunit mcgubbins -->
<script type="text/javascript" src="vendor/qunit-1.14.0.js"></script>

<script type="text/javascript" src="vendor/qunit-1.18.0.js"></script>
<link rel="stylesheet" href="vendor/qunit-1.18.0.css" />

<!-- Sinon -->
<script type="text/javascript" src="vendor/sinon-1.10.3.js"></script>
<script type="text/javascript" src="vendor/sinon-1.14.1.js"></script>

<!-- src -->
<script type="text/javascript" src="../src/logger.js"></script>
Expand All @@ -16,5 +17,7 @@

</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
</body>
</html>
Loading

0 comments on commit a69d7f7

Please sign in to comment.