Trying to log to file JavaScript Node.js / Express.js

Hello Everyone,

I hope this will be an easy one… but I can’t put my finger on the problem…

I’ve been trying for a while to make a log file of a few things on my glitch.

I’ve had a bug that needed some logging but could never figure out how to do it.Whatever this is irrelevant :wink: Cause I found the bug, but I would still love to understand how to:

  • Create a file
  • Log with timestamp in the file

I’ve tried a few options… I can trap the log on my console, but it doesn’t Output to a file.

I’ve tried the simple winston, work in console but doesn’t create a file…

Even just this on a new glitch Express doesn’t create the file.

Can anyone point me into the right direction?

And in the end I would love to make a Daily rotation of the file (winston-daily-rotate-file will probably be my next ting if I can create a file)

Unfortunately, done already. Closed and reopened the project… Same thing no file there…

You won’t obviously see the file, because it is deleted after 1 second:

setTimeout(function () {
  // Remove the file, ignoring any errors
  try { fs.unlinkSync(filename); }
  catch (ex) { }
}, 1000);

Delete lines, that includes fs.unlink, and you’ll have one persist .log file.

Still no files…

I’ll restart from the beginning… And trying options 1 by one to find out where the problem happens… Be back in 10 minutes

Have you checked through console? Glitch hides .log files by default.

Use ls -la to show all your files in directory.

I’ve managed to get the expected result:

'use strict'

const fs = require('fs')
const path = require('path')
const winston = require('winston')

const filename = path.join(__dirname, 'created-logfile.log')

const logger = winston.createLogger({
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename })
})'Hello created log files!', { 'foo': 'bar' })

1 Like

OMG thanks!!!

Now I understand… I probably ALWAYS created them… But if Glitch hides the .log files … I could never see it before…

OMG you are my hero XD