Skip to content

Instantly share code, notes, and snippets.

@CatTail
Last active October 21, 2015 08:47
Show Gist options
  • Save CatTail/56229409cb3ba17d43c0 to your computer and use it in GitHub Desktop.
Save CatTail/56229409cb3ba17d43c0 to your computer and use it in GitHub Desktop.
log-starter-kit examples
#! /usr/bin/env node
'use strict';
var app = require('koa')();
var port = 8080;
app.use(function* () {
console.log(this.url);
this.status = 200;
this.body = 'Hello World!';
});
app.listen(port);
console.log('Server listen at', port);
#! /usr/bin/env node
'use strict';
var util = require('util');
var moment = require('moment');
var uuid = require('node-uuid');
var app = require('koa')();
app.context.userid = function() {
var id = this.cookies.get('userID') || this.temporaryUuid;
if (!id) {
id = uuid.v4();
this.temporaryUuid = id;
this.cookies.set('userID', id, {expire: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000)});
}
return id;
};
app.use(function *accesslog(next) {
yield* next;
// ip, date, method, path, status and length
var format = '%s - %s [%s] "%s %s HTTP/1.X" %d %s';
var ip = this.req.headers['x-forwarded-for'] || this.req.connection.remoteAddress || '-';
var userid = this.userid();
var length = this.length ? this.length.toString() : '-';
var date = moment().format('D/MMM/YYYY:HH:mm:ss ZZ');
console.log(util.format(format, ip, userid, date, this.method, this.path, this.status, length));
});
app.use(function* () {
this.status = 200;
this.body = 'Hello World!';
});
var port = 8080;
app.listen(port);
console.log('Server listen at', port);
#! /usr/bin/env node
'use strict';
var util = require('util');
var moment = require('moment');
var _ = require('lodash');
var uuid = require('node-uuid');
var app = require('koa')();
var logger = require('./logger');
app.context.userid = function() {
var id = this.cookies.get('userID') || this.temporaryUuid;
if (!id) {
id = uuid.v4();
this.temporaryUuid = id;
this.cookies.set('userID', id, {expire: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000)});
}
return id;
};
app.context.log = function() {
var args = Array.prototype.slice.call(arguments);
if (typeof args[args.length-1] !== 'object') {
args.push({});
}
var meta = args[args.length-1];
meta.requestID = this.state.requestID;
logger.log.apply(logger, args);
};
app.use(function *accesslog(next) {
// generate requestID
this.state.requestID = uuid.v4();
yield* next;
// ip, date, method, path, status and length
var format = '%s - %s [%s] "%s %s HTTP/1.X" %d %s';
var ip = this.req.headers['x-forwarded-for'] || this.req.connection.remoteAddress || '-';
var userid = this.userid();
var length = this.length ? this.length.toString() : '-';
var date = moment().format('D/MMM/YYYY:HH:mm:ss ZZ');
this.log('info', util.format(format, ip, userid, date, this.method, this.path, this.status, length), {type: 'accesslog'});
this.log('info', _.pick(this.headers, ['referer', 'user-agent']), {type: 'headers'});
});
app.use(function* () {
this.status = 200;
this.log('info', 'Request logging', {type: 'sometype'});
this.body = 'Hello World!';
});
app.on("error", function(err) {
logger.error(this.url, {error: err});
});
var port = 8080;
app.listen(port);
logger.info('Server listen at', port);
'use strict';
var util = require('util');
var Buffer = require('buffer').Buffer;
var winston = require('winston');
var Sentry = require('winston-common-sentry');
var Scribe = require('winston-common-scribe');
var config = {
"debug": true,
"app": "log-starter-kit",
"sentry": "{{ TODO }}",
"scribe": {
"version": "1",
"host": "{{ TODO }}",
"port": 4252,
"category": "log-starter-kit"
},
"path": {
"log": "/tmp/log-starter-kit"
}
};
//
// Transports
//
var sentryTransport = new Sentry({
level: 'warn',
dsn: config.sentry,
});
var scribeTransport = new Scribe({
app: config.app,
version: config.scribe.version,
host: config.scribe.host,
port: config.scribe.port,
category: config.scribe.category,
});
var consoleTransport = new (winston.transports.Console)({
prettyPrint: true,
colorize: true,
});
var fileTransport = new (winston.transports.DailyRotateFile)({
colorize: false,
timestamp: true,
json: false,
filename: config.path.log,
maxsize: 1024 * 1024 * 100, // 100MB
maxFiles: 1,
datePattern: '.HH',
});
// https://github.com/winstonjs/winston/blob/master/docs/transports.md
var logger = new (winston.Logger)({
transports: config.debug ?
[consoleTransport, sentryTransport, scribeTransport] :
[fileTransport, sentryTransport, scribeTransport],
});
module.exports = logger;
{
"dependencies": {
"config": "^1.16.0",
"koa": "^1.0.0",
"lodash": "^3.10.1",
"moment": "^2.10.6",
"node-uuid": "^1.4.3",
"winston": "^1.0.1",
"winston-common-scribe": "^1.0.0",
"winston-common-sentry": "^0.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment