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"
PORT=3000
WEBSITE_URL="http://localhost:4200"
SESSION_SECRET="Please ⭐ this repository."
```
`backend/test/.env`
@ -87,4 +88,12 @@ NODE_ENV="dev"
PORT=3001
ROOT_ENDPOINT="http://localhost:3001"
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

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

View File

@ -1,12 +1,12 @@
{
"name": "@acrd/backend",
"version": "0.0.0",
"version": "0.6.0-pre-release",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@acrd/backend",
"version": "0.0.0",
"version": "0.6.0-pre-release",
"dependencies": {
"@acrd/ion": "github:acrdapp/ion",
"@acrd/types": "file:../frontend/src/types",
@ -42,7 +42,8 @@
"socket.io": "^4.0.0",
"striptags": "^3.2.0",
"typescript": "^4.2.3",
"winston": "^3.3.3"
"winston": "^3.3.3",
"yaml": "^2.3.1"
},
"devDependencies": {
"@types/bad-words": "^3.0.1",
@ -84,6 +85,7 @@
}
},
"../frontend/src/types": {
"name": "@acrd/types",
"hasInstallScript": true,
"dependencies": {
"typescript": "^4.9.4"
@ -7585,6 +7587,14 @@
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
"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": {
"version": "16.2.0",
"resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz",

View File

@ -48,7 +48,8 @@
"socket.io": "^4.0.0",
"striptags": "^3.2.0",
"typescript": "^4.2.3",
"winston": "^3.3.3"
"winston": "^3.3.3",
"yaml": "^2.3.1"
},
"devDependencies": {
"@types/bad-words": "^3.0.1",

View File

@ -2,6 +2,10 @@ import { connect } from 'mongoose';
import { config } from 'dotenv';
config();
import { parse } from 'yaml';
import fs from 'fs';
global['config'] = parse(fs.readFileSync('../config.yaml', 'utf-8'));
import './modules/deps';
import './modules/logger';
import { User } from './data/models/user';
@ -13,7 +17,7 @@ connect(process.env.MONGO_URI, {
useCreateIndex: true,
serverSelectionTimeoutMS: 0,
}).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 });
await User.updateMany({ $set: { status: 'OFFLINE' } })
});

View File

@ -18,7 +18,7 @@ export class Email {
});
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'));
this.email.use('compile', pugEngine({

View File

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

View File

@ -77,7 +77,7 @@ export default (app: Application) => {
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(expressSession({
secret: 'keyboard cat',
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: 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;
ROOT_ENDPOINT: string;
WEBSITE_URL: string;
SESSION_SECRET: string;
}
}
}
export {};
export { };

View File

@ -1,5 +1,17 @@
declare global {
const log: import('winston').Logger;
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'