Save logs in log files
// app.js var logger = require('morgan'); var fs = require('fs') var logDirectory = path.join(__dirname, 'log') fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory) var accesLogStream = FileStreamRotator.getStream({ date_format: 'YYYYMMDD', filename: path.join(logDirectory, 'access-%DATE%.log'), frequency: 'daily', verbose: false }) app.use(logger(logger.compile(`[joke] :req[User-Agent] :req[Referer]`), {stream: accesLogStream})) // Run Results // Request several times using postman [joke] PostmanRuntime/7.15.2 - [joke] PostmanRuntime/7.15.2 - [joke] PostmanRuntime/7.15.2 - [joke] PostmanRuntime/7.15.2 http://localhost:3000/dish
Save logs in mongodb
// models/log.js let {Schame} = {require('mongoose')} let Log = new Schema({ userAgent: { type: String, default: '' }, referrer: { type: String, default: '' } }) modole.exports = mongoose.model('Log', Log) // app.js var logger = require('morgan'); var log = require('./models/log.js') var writeToDB = { write: function (line) { var [, ua, re] = line.split(' ') var ele = new Log({ userAgent: ua, referrer: re }) ele.save(err => { if (err) { console.log('err', err) } }) } } /* morgan The source code for is written like this stream.write(line + '\n') So the write property of the stream property value specified for morgan is how the log is saved to the data */ app.use(logger(logger.compile(`[joke] :req[User-Agent] :req[Referer]`), {stream: writeToDB}))
Log a bug
The following error occurred when writing the log to mongodb:'E11000 duplicate key error collection: confusion.logs index: userAgent_1 DUP key: {: null}'
The solution I looked for online was db.addresses.createIndex ({"xmpp_id": 1}, {sparse: true}) which I didn't understand at the time.The problem was solved in a very violent way.The methods are as follows:
- Open mongodb's compass and open the Indexes panel for the corresponding collection.Remove index from error message.
- Open the Documents panel for the corresponding collection.Deleted index document without error message.(Don't have to stop using this method!!)