Open timpaulaskas opened 1 year ago
Do you get what you expect if you
console.log(`${global.__basedir}/logs/error.log`)
at the top of the file?
I think I just observed a similar or related error as well:
/var/log/script.log
(with sudo permissions mind you) no log-file is created.script.log
or even </path/to/script>/script.log
a log-file is created and progam continues without being stalled after a whileSo the issue (at least on my part) seems to stem from trying to log to a directory outside the script directory.
Also unlike for example #1958, I'm not using process.exit(...)
but let the script exit normally and set process.exitCode = ...
instead!
I found the culprit to be that the File-Transport expect its filename
parameter to be a file relative to its other (optional) parameter dirname
, as the transport will do join(dirname, filename)
to determine the actual path of the requested logfile target throughout its code.
But when you use an absolute path in filename
(without also setting dirname
) this join will of course fail, since eg. filename: /var/log/script.log
will be incorrectly joined to /var/log/var/log/script.log
.
Workaround: Set dirname: /var/log/
and filename: script.log
(or in the initial example: dirname: ${global.__basedir}/logs/
and filename: error.log
)
Solution: Fix all joins in https://github.com/winstonjs/winston/blob/master/lib/winston/transports/file.js to ignore dirname
when filename
is an absolute path, or (better) use this._basename
instead of this.filename
for the joins!
Workaround confirmed to work:
const logger = createLogger({
level: 'debug',
format: format.combine(format.splat(), format.simple(), format.json()),
transports: [
new transports.File({
filename: 'error.log',
dirname: `${global.__basedir}/logs`,
})
]
});
logger.add(new transports.Console({
format: format.combine(
format.colorize(),
format.simple()
)
}));
module.exports = logger;
// Example logs
logger.log('info', 'Hello simple log!');
logger.info('Hello log with metas', { color: 'blue' });
Also an update/notice about dirname vs filename to the documentation might be a good idea.
Wanted to note that I ran into this as well and adding the dirname property to go along with the filename property for the transport fixed my issue. Given that dirname was not in the docs for the File transport, would not have known to try this.
PR even just for docs would be greatly welcomed and appreciated!!!
🔎 Search Terms
Won't Create File
The problem
I can get the console transport to work and this create the Logs directory, but it doesn't create any log files and doesn't provide any error information.
What version of Winston presents the issue?
v3.8.2
What version of Node are you using?
v19.2.0
If this worked in a previous version of Winston, which was it?
No response
Minimum Working Example
Additional information
No response