feat: update deps, add logs to file

This commit is contained in:
Timofey Gelazoniya 2025-02-16 20:37:29 +03:00
parent 0fbea7cc3b
commit d9d20fd627
Signed by: zeldon
GPG Key ID: 047886915281DD2A
9 changed files with 2049 additions and 1578 deletions

View File

@ -1,2 +1,3 @@
NODE_ENV=development
LOG_LEVEL=trace
LOG_LEVEL=trace
LOG_FILE=false

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules
dist
.env
.env
logs

View File

@ -1,4 +1,4 @@
import { logger } from "#root/logger.js";
logger.info("Hello!");
logger.debug("I am a teapot");
logger.debug("I am a teapot");

3418
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,25 +1,29 @@
{
"main": "dist/index.js",
"type": "module",
"imports": {
"#root/*": "./dist/src/*"
},
"scripts": {
"dev": "npm run clean && tsc-watch --onSuccess \"tsx index.ts\"",
"build": "npm run clean && tsc --noEmit false",
"clean": "rimraf dist"
},
"devDependencies": {
"@types/node": "^20.12.12",
"rimraf": "^5.0.7",
"tsc-watch": "^6.2.0",
"tsx": "^4.11.0",
"typescript": "^5.4.5"
},
"dependencies": {
"dotenv": "^16.4.5",
"envalid": "^8.0.0",
"pino": "^9.1.0",
"pino-pretty": "^11.1.0"
}
"main": "dist/index.js",
"type": "module",
"imports": {
"#root/*": "./dist/src/*"
},
"scripts": {
"dev": "cross-env DEBUG=\"grammy*\" vite-node --watch index.ts",
"build": "vite build",
"start": "node dist/index.js",
"clean:npm": "rimraf dist",
"upgrade:npm": "rimraf package-lock.json && npm update --latest && npm install"
},
"devDependencies": {
"@types/node": "^22.13.4",
"cross-env": "^7.0.3",
"rimraf": "^6.0.1",
"typescript": "^5.7.3",
"vite": "^6.1.0",
"vite-node": "^3.0.5"
},
"dependencies": {
"dotenv": "^16.4.7",
"envalid": "^8.0.0",
"pino": "9.6.0",
"pino-pretty": "^13.0.0",
"pino-roll": "^3.0.0"
}
}

View File

@ -1,9 +1,14 @@
import 'dotenv/config';
import { cleanEnv, str } from 'envalid';
import "dotenv/config";
import { bool, cleanEnv, str } from "envalid";
export const env = cleanEnv(process.env, {
NODE_ENV: str({ choices: ["development", "production"] }),
LOG_LEVEL: str({
choices: ["trace", "debug", "info", "warn", "error", "fatal", "silent"],
}),
NODE_ENV: str({
choices: ["development", "production"],
default: "development",
}),
LOG_LEVEL: str({
choices: ["trace", "debug", "info", "warn", "error", "fatal", "silent"],
default: "info",
}),
LOG_FILE: bool({ default: false }),
});

View File

@ -1,20 +1,50 @@
import { env } from "#root/env.js";
import { LoggerOptions, pino } from "pino";
import PinoPretty, { PrettyOptions } from "pino-pretty";
import { resolve } from "node:path";
import pino, { LoggerOptions, TransportTargetOptions } from "pino";
const options: LoggerOptions = {
level: env.LOG_LEVEL
};
function createLoggerInstance() {
const transportTargets: TransportTargetOptions[] = [];
const prettyOptions: PrettyOptions = {
ignore: 'pid,hostname',
colorize: env.isDev ? true : false,
translateTime: 'SYS:dd.mm.yyyy, HH:MM:ss'
};
let consoleTarget: TransportTargetOptions = {
target: "pino-pretty",
level: env.LOG_LEVEL,
options: {
colorize: true,
ignore: "pid,hostname",
translateTime: "SYS:dd.mm.yyyy, HH:MM:ss",
},
};
export let logger = pino(options);
transportTargets.push(consoleTarget);
if (env.isDev) {
// @ts-ignore
logger = pino(options, PinoPretty(prettyOptions));
}
let fileTarget: TransportTargetOptions = {
target: "pino-roll",
level: env.LOG_LEVEL,
options: {
file: resolve("logs", "app"),
extension: ".log",
frequency: "daily",
size: "10m",
limit: {
count: 10,
},
mkdir: true,
},
};
if (env.LOG_FILE) transportTargets.push(fileTarget);
const options: LoggerOptions = {
level: env.LOG_LEVEL,
};
const transport = pino.transport({
targets: transportTargets,
});
const logger = pino(options, transport);
return logger;
}
export const logger = createLoggerInstance();

View File

@ -1,24 +1,31 @@
{
"compilerOptions": {
"strict": true,
"skipLibCheck": true,
"esModuleInterop": true,
"preserveWatchOutput": true,
"noEmit": true,
"module": "NodeNext",
"target": "ES2021",
"moduleResolution": "NodeNext",
"sourceMap": true,
"outDir": "dist",
"rootDir": ".",
"paths": {
"#root/*": [
"./src/*"
]
}
},
"include": [
"src/**/*",
"index.ts"
]
}
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"baseUrl": ".",
"paths": {
"#root/*": ["./src/*"]
},
"outDir": "dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"useUnknownInCatchVariables": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"exactOptionalPropertyTypes": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"skipLibCheck": true
},
"include": ["src/**/*", "index.ts"],
"exclude": ["node_modules", "dist"]
}

21
vite.config.ts Normal file
View File

@ -0,0 +1,21 @@
import { resolve } from "node:path";
import { defineConfig } from "vite";
export default defineConfig({
resolve: {
alias: {
"#root": resolve(__dirname, "./src"),
},
},
build: {
target: "node22",
outDir: "dist",
ssr: true,
rollupOptions: {
input: "index.ts",
output: {
format: "esm",
},
},
},
});