Skip to content

Instantly share code, notes, and snippets.

@MichinobuMaeda
Last active June 25, 2017 03:39
Show Gist options
  • Save MichinobuMaeda/a22c161d7612ab9a5d78132b166b40c6 to your computer and use it in GitHub Desktop.
Save MichinobuMaeda/a22c161d7612ab9a5d78132b166b40c6 to your computer and use it in GitHub Desktop.
how to store bunyan log stream into mongoose
const bunyan = require('bunyan')
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const Log = mongoose.model('Log', new Schema({
name: String,
hostname: String,
pid: Number,
level: Number,
msg: String,
time: { type: Date, index: true },
v: Number,
}, {
versionKey: 'ver',
}));
const MongooseLogStream = class {
write(rec) {
try { new Log(JSON.parse(rec)).save() }
catch(e) { console.error(e) }
}
}
const log = bunyan.createLogger({
name: "my log store",
streams: [{stream: new MongooseLogStream()}]
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment