1

I use Morgan and Express to provide REST APIs for my website. I implementet the morgan-logger this way in my main.js:

app.use(morgan('dev')); //logger
app.use(morgan('dev', {stream: fs.createWriteStream('./access.log', {flags: 
'a'})}));

Now it logs me every call to an API in my access.log file like this:

::ffff:10.60.101.165 - - [26/Feb/2019:08:19:57 +0000] "POST /api/api1
HTTP/1.1" 200 -
::ffff:10.60.101.165 - - [26/Feb/2019:08:19:58 +0000] "POST /api/api2
HTTP/1.1" 200 -
::ffff:10.60.101.165 - - [26/Feb/2019:08:19:58 +0000] "POST /api/api3
HTTP/1.1" 200 -

Thats fine, but when I write own log messages like console.log("error in file A"), it is printed in the console but not in my access.log file. How can I write manually to it inside my API file?

Stephan
  • 19
  • 3

2 Answers2

0

We use morgan logger for API logging. If you want to log anything then you can use winston logger library. Its pretty easy to use. I have already answered that here - Log4js javascript create daily log file

Also you can use pm2 to host your service. After that whenever you log anything on console, pm2 will write them in a file.

ManishKumar
  • 1,476
  • 2
  • 14
  • 22
0

To log everything use common instead of dev.

app.use(morgan('common')); //logger
app.use(morgan('common', {stream: fs.createWriteStream('./access.log', {flags: 
'a'})}));
Vivek Rahul
  • 314
  • 2
  • 15