-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinston-bookshelf-transport.js
61 lines (50 loc) · 1.74 KB
/
winston-bookshelf-transport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* winston-bookshelf-transport - A winston transport for use with bookshelfjs
*
* License : Public domain
* Author: Mike Qin
*/
var util = require('util'),
winston = require('winston');
//export name here can't be just "bookshelf" because of conflict
var BookshelfTransport = exports.BookshelfTransport = function(options) {
this.options = options || {};
if (!options.model)
throw new Error('A Bookshelf model used for logging is required');
};
// Inherit from `winston.Transport`.
util.inherits(BookshelfTransport, winston.Transport);
// Define a getter so that `winston.transports.Mysql`
// is available and thus backwards compatible.
winston.transports.BookshelfTransport = BookshelfTransport;
// Expose the name of this Transport on the prototype
BookshelfTransport.prototype.name = 'BookshelfTransport';
/**
*
* @param level {string} Level at which to log the message.
* @param msg {string} Message to log
* @param meta {Object} **Optional** Additional metadata to attach
* @param callback
*
Core logging method exposed to Winston. Metadata is optional.
*/
BookshelfTransport.prototype.log = function(level, msg, meta, callback) {
var self = this;
var model = self.options.model;
model.forge({level:level, message:msg, meta:meta, created_at: self.getDateTime()})
.save()
.then(function(log){
//can do something with the newly inserted log object here
self.emit('logged');
callback(null, true);
})
.catch(function(e){
self.emit('error', e);
console.error(e);
return callback(e, null);
});
};
BookshelfTransport.prototype.getDateTime = function(){
var now = new Date();
return now.toUTCString();
};