From b73e260e3506dd243501c0537eeb8dba4e9b09fd Mon Sep 17 00:00:00 2001 From: Microprocess Date: Thu, 28 Dec 2023 15:08:03 -0300 Subject: [PATCH] Add log --- .vscode/settings.json | 13 ----- docker-compose.yaml.example | 29 ---------- docker-compose.yaml.example.complete | 80 --------------------------- docker-compose.yaml.example.dockerhub | 28 ---------- src/config/env.config.ts | 4 ++ src/config/logger.config.ts | 46 ++++++++++++++- tsconfig.json | 3 +- 7 files changed, 51 insertions(+), 152 deletions(-) delete mode 100644 .vscode/settings.json delete mode 100644 docker-compose.yaml.example delete mode 100644 docker-compose.yaml.example.complete delete mode 100644 docker-compose.yaml.example.dockerhub diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 71db0b08..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "editor.fontSize": 13, - "editor.fontLigatures": true, - "editor.letterSpacing": 0.5, - "editor.smoothScrolling": true, - "editor.tabSize": 2, - "editor.codeActionsOnSave": { - "source.fixAll.eslint": "explicit", - "source.fixAll": "explicit" - }, - "prisma-smart-formatter.typescript.defaultFormatter": "esbenp.prettier-vscode", - "prisma-smart-formatter.prisma.defaultFormatter": "Prisma.prisma" -} \ No newline at end of file diff --git a/docker-compose.yaml.example b/docker-compose.yaml.example deleted file mode 100644 index d0a75a5d..00000000 --- a/docker-compose.yaml.example +++ /dev/null @@ -1,29 +0,0 @@ -version: '3.3' - -services: - api: - container_name: evolution_api - image: evolution/api:local - build: . - restart: always - ports: - - 8080:8080 - volumes: - - evolution_instances:/evolution/instances - - evolution_store:/evolution/store - networks: - - evolution-net - env_file: - - ./Docker/.env - command: ['node', './dist/src/main.js'] - expose: - - 8080 - -volumes: - evolution_instances: - evolution_store: - -networks: - evolution-net: - name: evolution-net - driver: bridge diff --git a/docker-compose.yaml.example.complete b/docker-compose.yaml.example.complete deleted file mode 100644 index de13de57..00000000 --- a/docker-compose.yaml.example.complete +++ /dev/null @@ -1,80 +0,0 @@ -version: '3.3' - -services: - api: - container_name: evolution_api - image: evolution/api:local - build: . - restart: always - ports: - - 8080:8080 - volumes: - - evolution_instances:/evolution/instances - - evolution_store:/evolution/store - networks: - - evolution-net - env_file: - - ./Docker/.env - command: ['node', './dist/src/main.js'] - expose: - - 8080 - - mongodb: - container_name: mongodb - image: mongo - restart: always - ports: - - 27017:27017 - environment: - - MONGO_INITDB_ROOT_USERNAME=root - - MONGO_INITDB_ROOT_PASSWORD=root - - PUID=1000 - - PGID=1000 - volumes: - - evolution_mongodb_data:/data/db - - evolution_mongodb_configdb:/data/configdb - networks: - - evolution-net - expose: - - 27017 - - mongo-express: - image: mongo-express - networks: - - evolution-net - environment: - ME_CONFIG_BASICAUTH_USERNAME: root - ME_CONFIG_BASICAUTH_PASSWORD: root - ME_CONFIG_MONGODB_SERVER: mongodb - ME_CONFIG_MONGODB_ADMINUSERNAME: root - ME_CONFIG_MONGODB_ADMINPASSWORD: root - ports: - - 8081:8081 - links: - - mongodb - - redis: - image: redis:latest - container_name: redis - command: > - redis-server - --port 6379 - --appendonly yes - volumes: - - evolution_redis:/data - networks: - - evolution-net - ports: - - 6379:6379 - -volumes: - evolution_instances: - evolution_store: - evolution_mongodb_data: - evolution_mongodb_configdb: - evolution_redis: - -networks: - evolution-net: - name: evolution-net - driver: bridge diff --git a/docker-compose.yaml.example.dockerhub b/docker-compose.yaml.example.dockerhub deleted file mode 100644 index b33e8f4a..00000000 --- a/docker-compose.yaml.example.dockerhub +++ /dev/null @@ -1,28 +0,0 @@ -version: '3.3' - -services: - api: - container_name: evolution_api - image: atendai/evolution-api:latest - restart: always - ports: - - 8080:8080 - volumes: - - evolution_instances:/evolution/instances - - evolution_store:/evolution/store - networks: - - evolution-net - env_file: - - ./Docker/.env - command: ['node', './dist/src/main.js'] - expose: - - 8080 - -volumes: - evolution_instances: - evolution_store: - -networks: - evolution-net: - name: evolution-net - driver: bridge diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 511f4ebc..5d08cb34 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -26,6 +26,8 @@ export type Log = { LEVEL: LogLevel[]; COLOR: boolean; BAILEYS: LogBaileys; + LOG_PATH: string; + LOG_EXPIRATION_DAYS: number; }; export type SaveData = { @@ -265,6 +267,8 @@ export class ConfigService { ], COLOR: process.env?.LOG_COLOR === 'true', BAILEYS: (process.env?.LOG_BAILEYS as LogBaileys) || 'error', + LOG_PATH: join(__dirname, '../../') + process.env?.LOG_PATH || join(__dirname, '../../')+'log', + LOG_EXPIRATION_DAYS: Number.parseInt(process.env?.LOG_EXPIRATION_DAYS), }, DEL_INSTANCE: isBooleanString(process.env?.DEL_INSTANCE) ? process.env.DEL_INSTANCE === 'true' diff --git a/src/config/logger.config.ts b/src/config/logger.config.ts index 221ee098..c4e58306 100644 --- a/src/config/logger.config.ts +++ b/src/config/logger.config.ts @@ -1,5 +1,6 @@ import dayjs from 'dayjs'; -import fs from 'fs'; +import fs, { mkdir } from 'fs'; +import util from 'util'; import { configService, Log } from './env.config'; const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')); @@ -70,6 +71,18 @@ export class Logger { this.configService.get('LOG').LEVEL.forEach((level) => types.push(Type[level])); const typeValue = typeof value; + + salvarLog(this.configService.get('LOG'), + '[Evolution API]'+ + process.pid.toString()+ + '-'+ + `${formatDateLog(Date.now())} `+ + `${type} `+ + `[${this.context}]`+ + `[${typeValue}]`+ + value + ); + if (types.includes(type)) { if (configService.get('LOG').COLOR) { console.log( @@ -139,3 +152,34 @@ export class Logger { this.console(value, Type.DARK); } } + +function salvarLog(env: any, log: string): void { + mkdir(env.LOG_PATH, { recursive: true }, (err) => { if (err) throw err; }); + let file = new Date().toLocaleDateString().replaceAll('/', ''); + file = env.LOG_PATH + '/' + file + '.txt'; + try { + if (fs.existsSync(file)) { + fs.appendFileSync(file, log, "utf8"); + } else { + fs.writeFileSync(file, log); + } + excluirArquivosAntigos(env.LOG_PATH, 5); + } catch (accessError) { + console.error('Erro ao salvar o log:', accessError); + } + + +} + +function excluirArquivosAntigos(path: string, diasLimite: number): void { + const data = new Date(); + data.setDate(data.getDate() - diasLimite); + let limite = Number.parseInt(data.toLocaleDateString().replaceAll('/', '')); + fs.readdirSync(path).forEach((nomeArquivo) => { + let file = Number.parseInt(nomeArquivo.replace(/^.*[\\\/]/, '').replace(/\.[^/.]+$/, '')); + if (file < limite) { + fs.unlinkSync(path + '/' + nomeArquivo); + console.log(`Arquivo ${nomeArquivo} excluĂ­do.`); + } + }); +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 0c89eea1..c22728f4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,7 +16,8 @@ "skipLibCheck": true, "strictNullChecks": false, "incremental": true, - "noImplicitAny": false + "noImplicitAny": false, + "lib": ["ES2021.String"] }, "exclude": ["node_modules", "./test", "./dist", "./prisma"] } \ No newline at end of file