init
This commit is contained in:
37
src/logger.ts
Normal file
37
src/logger.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { appendFileSync, existsSync } from 'fs';
|
||||
import { createWriteStream } from 'fs';
|
||||
|
||||
const filename = 'logs.csv',
|
||||
exist = existsSync(filename);
|
||||
|
||||
console.log(filename, exist);
|
||||
|
||||
if (!exist) {
|
||||
|
||||
appendFileSync(filename, ['Date', 'Method', 'Url', 'Status'].join(',') + '\n', 'utf8');
|
||||
}
|
||||
|
||||
export const log = (method: string, url: string, status: number) => {
|
||||
appendFileSync(filename, [isoDateInTimeZone(), method, url, status].join(',') + '\n', 'utf8');
|
||||
}
|
||||
|
||||
function isoDateInTimeZone(timeZone = 'America/Buenos_Aires', date = new Date()) {
|
||||
const parts = new Intl.DateTimeFormat("en-CA", {
|
||||
timeZone,
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
fractionalSecondDigits: 3,
|
||||
hour12: false,
|
||||
timeZoneName: "shortOffset",
|
||||
}).formatToParts(date),
|
||||
map = Object.fromEntries(parts.map(p => [p.type, p.value])),
|
||||
offset = map.timeZoneName.replace("GMT", "");
|
||||
|
||||
return `${map.year}-${map.month}-${map.day}T` +
|
||||
`${map.hour}:${map.minute}:${map.second}.` +
|
||||
`${map.fractionalSecond}${offset}`;
|
||||
}
|
||||
Reference in New Issue
Block a user