Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Better logging for Glog #27

Open
wants to merge 5 commits into from

2 participants

Guy Halford-Thompson Will Laurance
Guy Halford-Thompson
Owner

cc @wlaurance

I am not overly keen on bringing additional dependencies into the project, but at the same time it would be nice to have better logging in Glog. I created a small Logging library for use with one of the other projects, and am proposing to integrate it into Glog.

The library is called (Glogger)[https://github.com/guyht/glogger] and is a very bare bones wrapper around (winston)[https://github.com/flatiron/winston], setting up some default levels and prompts. It also has built in Loggly support.

Putting this forward as a candidate for integration into the master branch of Glog.

Will Laurance

:+1: lgtm, I have used winston before and loggly looks easy and free.

Will Laurance

@guyht any progress on glogger?

Guy Halford-Thompson
Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 23, 2013
  1. Added port for appfog

    authored
  2. Use glogger logging package

    authored
  3. Stringify header

    authored
  4. Added gloggler to package.json

    authored
  5. Reverting bad commit

    Guy Halford-Thompson authored
This page is out of date. Refresh to see the latest.
Showing with 31 additions and 26 deletions.
  1. +23 −23 lib/glog.js
  2. +2 −1  package.json
  3. +6 −2 server.js
46 lib/glog.js
View
@@ -10,6 +10,8 @@ var path = require('path'),
async = require('async'),
config = require('config');
+// Custom log package
+var log = require('glogger')('GLOG');
/*
* Setup module
@@ -61,8 +63,6 @@ Glog.prototype.load_configs = function(cb) {
options[key] = config[key];
}
- console.log(JSON.stringify(options));
-
cb(options);
}
@@ -102,8 +102,8 @@ Glog.prototype.load_plugins = function(options, cb) {
var p = require('../plugins/' + options.plugins[i]);
p.load(GlogPlugin);
} catch(e) {
- console.log('Error loading plugins');
- console.log(e);
+ log.info('Error loading plugins');
+ log.info(e);
}
}
@@ -120,7 +120,7 @@ Glog.prototype.load_articles = function(options, cb) {
article_path = path.join('blog_repo', '/articles'),
fn = this;
- console.log('Loading articles from ' + article_path);
+ log.info('Loading articles from ' + article_path);
fs.readdir(article_path, function(err, files) {
if(err) {
@@ -130,14 +130,14 @@ Glog.prototype.load_articles = function(options, cb) {
// Remove incorrect extensions
for(var i=0; i<files.length; i++) {
if(path.extname(files[i]) !== '.txt' && path.extname(files[i]) !== '.md') {
- console.log('Skipping file ' + files[i] + ' Incorrect extension');
+ log.info('Skipping file ' + files[i] + ' Incorrect extension');
files.splice(i,1);
}
}
for(var i=0; i<files.length; i++) {
- console.log('Reading file ' + files[i]);
+ log.info('Reading file ' + files[i]);
fs.readFile(path.join(article_path, files[i]), 'UTF-8', function(err, data) {
if(err) {
@@ -150,8 +150,8 @@ Glog.prototype.load_articles = function(options, cb) {
parts.splice(0,1);
var body = parts.join('\n\n');
- console.log('Read articles with headers: ');
- console.log(header);
+ log.info('Read articles with headers: ');
+ log.info(JSON.stringify(header));
// Check mandatory fields
if(!header.title) {
@@ -193,7 +193,7 @@ Glog.prototype.load_articles = function(options, cb) {
var postlength = Number(fn.articlePostPlugins.length);
var prelength = Number(fn.articlePrePlugins.length);
if(fn.articlePrePlugins.length > 1){
- console.log('preplugins');
+ log.info('preplugins');
fn.articlePrePlugins[0] = function(wcb){
wcb(null, articles);
}
@@ -296,7 +296,7 @@ Glog.prototype.render_blog = function(options, articles, cb) {
fn.handle_error('Error loading layout. Err: ' + err);
}
- console.log('Compiling template for home page');
+ log.info('Compiling template for home page');
var ja = jade.compile(data),
per_page = options.articles_per_page,
i, len = Math.floor((articles.length / per_page) + 1),
@@ -319,7 +319,7 @@ Glog.prototype.render_blog = function(options, articles, cb) {
fn.pages['/'] = fn.pages['_page1'];
}
- console.log('Compiling template for individual pages');
+ log.info('Compiling template for individual pages');
// Loop through articles and compile individual pages
for(var i=0; i< articles.length; i++) {
@@ -344,11 +344,11 @@ Glog.prototype.render_blog = function(options, articles, cb) {
* Handle a request for the home page
*/
Glog.prototype.req_home = function(req, res, next, options) {
- console.log('Home page requested');
+ log.info('Home page requested');
var pagenum = req.params.pagenum || 1;
- console.log('Rendering page ' + pagenum);
+ log.info('Rendering page ' + pagenum);
// Set headers
res.setHeader('Content-Type', 'text/html; charset=utf-8');
@@ -367,7 +367,7 @@ Glog.prototype.req_home = function(req, res, next, options) {
* Handle a request for the RSS feed
*/
Glog.prototype.req_rss = function(req, res, next, options) {
- console.log('RSS feed requested');
+ log.info('RSS feed requested');
// Set headers
res.setHeader('Content-Type', 'application/rss+xml; charset=utf-8');
@@ -386,7 +386,7 @@ Glog.prototype.req_article = function(req, res, next, options) {
var fn = this,
url = [req.params.year, req.params.month, req.params.article].join('/');
- console.log('Requesting ' + url);
+ log.info('Requesting ' + url);
if(typeof this.pages[url] === 'undefined') {
// Set headers
res.setHeader('Content-Type', 'text/html; charset=utf-8');
@@ -407,7 +407,7 @@ Glog.prototype.req_article = function(req, res, next, options) {
* Generic error handler
*/
Glog.prototype.handle_error = function(err) {
- console.error(err);
+ log.error(err);
}
@@ -443,14 +443,14 @@ Glog.prototype.get_repo = function(repo, cb) {
var fn = this;
// Attempt to clone the git repo
- console.log('Cloning git repository ' + repo);
+ log.info('Cloning git repository ' + repo);
exec('git clone ' + repo + ' blog_repo', function(error, stdout, stderr) {
if(error) {
fn.handle_error('Could not clone git repository: ' + error);
cb(false);
}
- console.log('Stdout: ' + stdout);
- console.log('Stderr: ' + stderr);
+ log.info('Stdout: ' + stdout);
+ log.info('Stderr: ' + stderr);
cb(true);
});
@@ -474,14 +474,14 @@ Glog.prototype.update_repo = function(options, cb) {
fn.get_repo(options.blog_repository, cb);
} else {
- console.log('Updating blog git repo');
+ log.info('Updating blog git repo');
exec('cd blog_repo; git pull origin master', function(error, stdout, stderr) {
if(err) {
fn.handle_error('Could not update blog repo: ' + error);
cb(false);
}
- console.log('Stdout: ' + stdout);
- console.log('Stderr: ' + stderr);
+ log.info('Stdout: ' + stdout);
+ log.info('Stderr: ' + stderr);
cb(true);
});
3  package.json
View
@@ -10,7 +10,8 @@
"mocha": ">= 0.8.2",
"rss": ">= 0.0.3",
"async": ">= 0.1.22",
- "config": "~0.4.17"
+ "config": "~0.4.17",
+ "gloggler" : "git://github.com/guyht/glogger.git"
},
"devDependencies": {
"underscore": "~1.4.2"
8 server.js
View
@@ -2,15 +2,19 @@ var glog = require('./lib/glog'),
express = require('express'),
path = require('path');
+// Custom log package
+var log = require('glogger')('SERVER');
+
glog.rebuild(function() {
glog.load_configs(function(options) {
- console.log('Starting server on port ' + options.port);
+ log.info('Starting server on port ' + options.port);
var server = express.createServer(
express.static(path.join('blog_repo', '/public')),
express.staticCache(),
+ express.logger({'format': 'tiny', 'stream': log.stream()}),
express.router(function(app) {
app.get('/__render', function(req, res, next) {
glog.rebuild(function() {
@@ -38,6 +42,6 @@ glog.rebuild(function() {
glog.req_home(req, res, next, options)
});
})
- ).listen(options.port);
+ ).listen(process.env.VCAP_APP_PORT || options.port);
});
});
Something went wrong with that request. Please try again.