I'm trying to figure out what's the best way to improve messages being logged to the terminal from a running jake task.
I want to be able to show nicely formatted messages through the terminal from, not only the running server but any other tasks that may be running (including invoked from other tasks). For example, say I execute jake run to have the server run while I develop, that also invokes the jake watch task that monitors my less css file for changes. It may also invoke jake unit (not shown in example above) for continuously running unit tests, etc...
I think having the logger log to file (say ./dev.log) and simply running some type of tail -f from within that jake run task would make sense (with the added benefit of now having a file log. If that's a reasonable way to go, then:
./dev.log to the running task/terminal?If not, and you have a better suggestion, could you pint me in the right direction? If and once implemented, I'll share the solution here.
Thank you!
var servers = [
{
root: 'application',
port: 8000
},
{
root: 'dist',
port: 8001
}
];
// jake run
desc('Run when coding. (Watches files, compiles less, runs dev server, runs test, etc)');
task('run', {async: true}, function () {
jake.Task.watch.invoke(); // invoking the 'watch' teask which I keep separate
jake.logger.log('Starting static web servers:'); // simple logging through jake (this is what I'm trying to replace)
var connect = require('connect');
servers.forEach(function (server) {
jake.logger.log('localhost:' + server.port + ' -> ' + server.root);
connect(connect['static'](server.root)).listen(server.port);
});
});
desc('Watches for changes to the main .less file');
task('watch', function () {
var gaze = require('gaze');
gaze(['application/css/style.less'], function (err, watcher) {
this.on('all', function (event, filepath) {
var fs = require('fs');
var less = require('./node_modules/less/lib/less');
var parser = new (less.Parser)({
paths: ['./application/css'], // Specify search paths for @import directives
filename: 'style.less'
});
parser.parse(fs.readFileSync('./application/css/style.less', 'utf8'), function (err, tree) {
if (err) {
return console.error(err);
// TODO log error
} else {
fs.writeFileSync('./application/css/style.css', tree.toCSS({ compress: true }));
jake.logger.log('application/css/style.less > application/css/style.css');
complete();
// TODO log success
}
});
});
});
});
Found one solution: https://github.com/flatiron/winston
var colors = require('colors'); // in case I want to use color in messages
var winston = require('winston');
var logger = new (winston.Logger)({
exitOnError: false,
transports: [
new (winston.transports.Console)({
colorize: true,
handleExceptions: true
})
]
});
logger.cli();
Then in my tasks:
logger.info('application/css/style.less > application/css/style.css');
or
logger.error("[" + err.type + "] [" + err.filename + ":" + err.line + "," + err.column + "]: " + err.message);
This works fine for me. It'd be better if I could pass a formatter function to the logger so I could avoid using colors over and over but no biggie.
Thanks anyways!