feat: update deps, add logs to file
This commit is contained in:
parent
0fbea7cc3b
commit
d9d20fd627
@ -1,2 +1,3 @@
|
||||
NODE_ENV=development
|
||||
LOG_LEVEL=trace
|
||||
LOG_LEVEL=trace
|
||||
LOG_FILE=false
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
node_modules
|
||||
dist
|
||||
.env
|
||||
.env
|
||||
logs
|
2
index.ts
2
index.ts
@ -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
3418
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
50
package.json
50
package.json
@ -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"
|
||||
}
|
||||
}
|
||||
|
17
src/env.ts
17
src/env.ts
@ -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 }),
|
||||
});
|
||||
|
@ -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();
|
||||
|
@ -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
21
vite.config.ts
Normal 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",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user