I generated fastify project from fastify cli then I really want to use the logger system and write it into the file
The code below is the way I try but when I send the request it does not write any log file yet and the system does not create any file also.
import { join } from 'path';
import AutoLoad, {AutoloadPluginOptions} from 'fastify-autoload';
import { FastifyLoggerOptions, FastifyPluginAsync } from 'fastify';
export type AppOptions = {
// Place your custom options for app below here.
} & Partial<AutoloadPluginOptions> & Partial<FastifyLoggerOptions>;
const app: FastifyPluginAsync<AppOptions> = async (
fastify,
opts
): Promise<void> => {
// Place here your custom code!
opts.level = 'info'
opts.file = 'logger'
fastify.log.info('Test log')
// Do not touch the following lines
// This loads all plugins defined in plugins
// those should be support plugins that are reused
// through your application
void fastify.register(AutoLoad, {
dir: join(__dirname, 'plugins'),
options: opts
})
// This loads all plugins defined in routes
// define your routes in one of these
void fastify.register(AutoLoad, {
dir: join(__dirname, 'routes'),
options: opts
})
};
export default app;
export { app }
the opts.file is the place I put the file path to write all the log of the request.