Preview:
const { format } = require('date-fns');
const { v4: uuid } = require('uuid'); // import version 4 as uuid

const fs = require('fs');
const fsPromises = require('fs').promises;
const path = require('path');

const logEvents = async (message, logName) => {
  const dateTime = `${format(new Date(), 'yyyyMMdd\tHH:mm:ss')}`;
  const logItem = `${dateTime}\t${uuid()}\t${message}\n`;
  console.log(logItem);
  try {
    if (!fs.existsSync(path.join(__dirname, 'logs'))) {
      await fsPromises.mkdir(path.join(__dirname, 'logs'));
    } // if the logs folder does not exist, create it
    await fsPromises.appendFile(path.join(__dirname, 'logs', logName), logItem); 
  } catch (err) {
    console.error(err);
  }
}

module.exports = logEvents;
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter