In summary, I am trying to log requests and responses to a file, a simple text file. This would be as simple as:
export default defineEventHandler(async (event) => {
try {
const logsDir = join(process.cwd(), 'logs');
await fs.mkdir(logsDir, {recursive: true});
await fs.writeFile(join(logsDir, 'log.txt'), 'testing');
console.log('success');
} catch (error) {
console.error(error);
}
});
This is at server/middleware/log.ts
This should work, however, nothing is getting saved to logs/log.txt. The folder doesn't even get created. Console gets a success message, and no errors occur. This is happening on my local windows development environment.
My goal is to add some form of logging to a file found on the server, within its working directory. I have a custom fetch that is used to connect to an external api. I also have a proxy set up in the nuxt config to point the custom fetch to the correct external api location. I want to record any errors that are received from the api to a file. I can use any logging package, such as pino and winston, however i tried briefly to get it to work and had a few errors from winston so just thought i just need a simple write to file at the moment and nothing major.
If you have any other ideas or suggestions, i'm all ears. Thanks!