Customize console logs via config.yaml.

This commit is contained in:
theADAMJR 2023-05-30 19:58:22 +01:00
parent d73dc2133d
commit f29a14a542
12 changed files with 73 additions and 17 deletions

View File

@ -73,6 +73,7 @@ MONGO_URI="mongodb://database/accord"
NODE_ENV="dev" NODE_ENV="dev"
PORT=3000 PORT=3000
WEBSITE_URL="http://localhost:4200" WEBSITE_URL="http://localhost:4200"
SESSION_SECRET="Please ⭐ this repository."
``` ```
`backend/test/.env` `backend/test/.env`
@ -87,4 +88,12 @@ NODE_ENV="dev"
PORT=3001 PORT=3001
ROOT_ENDPOINT="http://localhost:3001" ROOT_ENDPOINT="http://localhost:3001"
WEBSITE_URL="http://localhost:4200" WEBSITE_URL="http://localhost:4200"
SESSION_SECRET="Please ⭐ this repository."
``` ```
## Troubleshooting
### App does not connect to MongoDB on Windows?
- Ensure MongoDB is installed.
- If localhost does not work, use `mongodb://127.0.0.1:27017/accord`.
- https://stackoverflow.com/a/73139137/8304458

View File

@ -3,4 +3,5 @@ EMAIL_PASSWORD="password"
MONGO_URI="mongodb://localhost/accord" MONGO_URI="mongodb://localhost/accord"
NODE_ENV="dev" NODE_ENV="dev"
PORT=3000 PORT=3000
WEBSITE_URL="http://localhost:4200" WEBSITE_URL="http://localhost:4200"
SESSION_SECRET="Please ⭐ this repository."

View File

@ -1,12 +1,12 @@
{ {
"name": "@acrd/backend", "name": "@acrd/backend",
"version": "0.0.0", "version": "0.6.0-pre-release",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@acrd/backend", "name": "@acrd/backend",
"version": "0.0.0", "version": "0.6.0-pre-release",
"dependencies": { "dependencies": {
"@acrd/ion": "github:acrdapp/ion", "@acrd/ion": "github:acrdapp/ion",
"@acrd/types": "file:../frontend/src/types", "@acrd/types": "file:../frontend/src/types",
@ -42,7 +42,8 @@
"socket.io": "^4.0.0", "socket.io": "^4.0.0",
"striptags": "^3.2.0", "striptags": "^3.2.0",
"typescript": "^4.2.3", "typescript": "^4.2.3",
"winston": "^3.3.3" "winston": "^3.3.3",
"yaml": "^2.3.1"
}, },
"devDependencies": { "devDependencies": {
"@types/bad-words": "^3.0.1", "@types/bad-words": "^3.0.1",
@ -84,6 +85,7 @@
} }
}, },
"../frontend/src/types": { "../frontend/src/types": {
"name": "@acrd/types",
"hasInstallScript": true, "hasInstallScript": true,
"dependencies": { "dependencies": {
"typescript": "^4.9.4" "typescript": "^4.9.4"
@ -7585,6 +7587,14 @@
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="
}, },
"node_modules/yaml": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.3.1.tgz",
"integrity": "sha512-2eHWfjaoXgTBC2jNM1LRef62VQa0umtvRiDSk6HSzW7RvS5YtkabJrwYLLEKWBc8a5U2PTSCs+dJjUTJdlHsWQ==",
"engines": {
"node": ">= 14"
}
},
"node_modules/yargs": { "node_modules/yargs": {
"version": "16.2.0", "version": "16.2.0",
"resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz",

View File

@ -48,7 +48,8 @@
"socket.io": "^4.0.0", "socket.io": "^4.0.0",
"striptags": "^3.2.0", "striptags": "^3.2.0",
"typescript": "^4.2.3", "typescript": "^4.2.3",
"winston": "^3.3.3" "winston": "^3.3.3",
"yaml": "^2.3.1"
}, },
"devDependencies": { "devDependencies": {
"@types/bad-words": "^3.0.1", "@types/bad-words": "^3.0.1",
@ -88,4 +89,4 @@
"ts-node": "^10.4.0", "ts-node": "^10.4.0",
"ts-node-dev": "^1.1.8" "ts-node-dev": "^1.1.8"
} }
} }

View File

@ -2,6 +2,10 @@ import { connect } from 'mongoose';
import { config } from 'dotenv'; import { config } from 'dotenv';
config(); config();
import { parse } from 'yaml';
import fs from 'fs';
global['config'] = parse(fs.readFileSync('../config.yaml', 'utf-8'));
import './modules/deps'; import './modules/deps';
import './modules/logger'; import './modules/logger';
import { User } from './data/models/user'; import { User } from './data/models/user';
@ -13,7 +17,7 @@ connect(process.env.MONGO_URI, {
useCreateIndex: true, useCreateIndex: true,
serverSelectionTimeoutMS: 0, serverSelectionTimeoutMS: 0,
}).catch(error => log.error(error.message || 'Unable to connect to db', { uri: process.env.MONGO_URI })) }).catch(error => log.error(error.message || 'Unable to connect to db', { uri: process.env.MONGO_URI }))
.then(async (con) => { .then(async () => {
log.info(`Connected to database.`, { uri: process.env.MONGO_URI }); log.info(`Connected to database.`, { uri: process.env.MONGO_URI });
await User.updateMany({ $set: { status: 'OFFLINE' } }) await User.updateMany({ $set: { status: 'OFFLINE' } })
}); });

View File

@ -18,7 +18,7 @@ export class Email {
}); });
this.email.verify((error) => (error) this.email.verify((error) => (error)
? log.error(error) ? log.error(error?.message || 'Failed to login to email service')
: log.info('Logged in to email service')); : log.info('Logged in to email service'));
this.email.use('compile', pugEngine({ this.email.use('compile', pugEngine({

View File

@ -7,12 +7,12 @@ const colorsAndTime = format.combine(
); );
addColors({ addColors({
info: 'bold blue', info: global.config.logger.info,
warn: 'bold yellow', warn: global.config.logger.warn,
error: 'bold red', error: global.config.logger.error,
debug: 'bold green', debug: global.config.logger.debug,
verbose: 'bold grey', verbose: global.config.logger.verbose,
silly: 'bold magenta', silly: global.config.logger.silly,
}); });
const logger = createLogger({ const logger = createLogger({

View File

@ -77,7 +77,7 @@ export default (app: Application) => {
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(passport.initialize()); app.use(passport.initialize());
app.use(expressSession({ app.use(expressSession({
secret: 'keyboard cat', secret: process.env.SESSION_SECRET,
resave: false, resave: false,
saveUninitialized: true, saveUninitialized: true,
cookie: { secure: true } cookie: { secure: true }

View File

@ -0,0 +1,9 @@
API_URL="http://localhost:3001/api"
EMAIL_ADDRESS="...@gmail.com"
EMAIL_PASSWORD="..."
MONGO_URI="mongodb://localhost/accord-test"
NODE_ENV="dev"
PORT=3001
ROOT_ENDPOINT="http://localhost:3001"
WEBSITE_URL="http://localhost:4200"
SESSION_SECRET="Please ⭐ this repository."

View File

@ -8,7 +8,8 @@ declare global {
PORT: string; PORT: string;
ROOT_ENDPOINT: string; ROOT_ENDPOINT: string;
WEBSITE_URL: string; WEBSITE_URL: string;
SESSION_SECRET: string;
} }
} }
} }
export {}; export { };

View File

@ -1,5 +1,17 @@
declare global { declare global {
const log: import('winston').Logger; const log: import('winston').Logger;
const deps: import('../src/modules/deps').Deps; const deps: import('../src/modules/deps').Deps;
const config: ConfigYAML;
interface ConfigYAML {
logger: {
info: string,
warn: string,
error: string,
debug: string,
verbose: string,
silly: string,
}
}
} }
export {} export { }

9
config.yaml Normal file
View File

@ -0,0 +1,9 @@
# This config is public. Use .env for private and secure values.
# See https://www.npmjs.com/package/winston for custom console log formatting.
logger:
info: 'bold blue'
warn: 'bold yellow'
error: 'bold red'
debug: 'bold green'
verbose: 'bold grey'
silly: 'bold magenta'