Skip to content

Commit

Permalink
Merge pull request #285 from LeoDog896/auto-log
Browse files Browse the repository at this point in the history
fix(relay): create logs directory before logging attempt
  • Loading branch information
matlorr authored Feb 27, 2025
2 parents 9ac22a8 + 268817c commit 38c28fa
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions relay/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,22 @@ const server = app
const lang = req.params.lang

const ip = anonymize(req.headers['x-forwarded-for'] || req.socket.remoteAddress)
const log = `${process.cwd()}/logs/game-access.log`
const log = path.join(process.cwd(), 'logs', 'game-access.log')
const header = "date;anon-ip;game;lang\n"
const data = `${new Date()};${ip};${owner}/${repo};${lang}\n`

fs.writeFile(log, header.concat(data), { flag: 'ax' }, (file_exists) => {
if (file_exists) {
fs.appendFile(log, data, (err) => {
if (err) console.log("Failed to append to log!")
});
}
fs.mkdir(path.join(process.cwd(), 'logs'), { recursive: true }, (err) => {
if (err) console.log("Failed to create logs directory!")
else {
// 'ax' fails if the file already exists: https://nodejs.org/api/fs.html#file-system-flags
fs.writeFile(log, header.concat(data), { flag: 'ax' }, (file_exists) => {
if (file_exists) {
fs.appendFile(log, data, (err) => {
if (err) console.log(`Failed to append to log: ${err}`)
});
}
});
}
});

console.log(`[${new Date()}] ${ip} requested translation for ${owner}/${repo} in ${lang}`)
Expand Down

0 comments on commit 38c28fa

Please sign in to comment.