Skip to content

Commit

Permalink
logger feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tugrul committed Jun 22, 2021
1 parent 11ec242 commit 914f1dc
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ APP_PORT=3000
NODE_ENV=production
REDIS_URL=redis://db
CRYPTLEX_API_BASE_URL=https://api.cryptlex.com
CRYPTLEX_API_ACCESS_TOKEN=
CRYPTLEX_API_ACCESS_TOKEN=
LOGS_DIR=logs
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules/
/data/dump.rdb
.env
.env
/logs/*.log
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const redisClient = new RedisPromise(redis.createClient(process.env.REDIS_URL));
const redlock = new Redlock([redisClient.originalClient]);
const doClient = new DigitalOcean(process.env.DO_API_TOKEN);


const logger = require('./logger');

const clientAuthChecker = authChecker(
async (token) => await isValidLicenseId(token)
Expand Down Expand Up @@ -251,6 +251,7 @@ router.post('/get-license-id', wrap(async(req, res) => {
const licenseId = await getLicenseIdByLicenseKey(licenseKey);
res.json({success: true, licenseId});
} catch (err) {
logger.error('get license id endpoint', err);
res.json({success: false, message: 'an error occured during license key checking'});
}

Expand Down
30 changes: 30 additions & 0 deletions logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

const {createLogger, format, transports} = require('winston');

const {NODE_ENV, LOGS_DIR} = process.env;

function getLogPath(fileName) {
if (LOGS_DIR.length === 0) {
return fileName;
}

return LOGS_DIR + (/\/$/.test(LOGS_DIR) ? '' : '/') + fileName;
}

const logger = createLogger({
level: 'info',
format: format.json(),
defaultMeta: {service: 'subdomain-provider-api'},
transports: [
new transports.File({filename: getLogPath('error.log'), level: 'error'}),
new transports.File({filename: getLogPath('combined.log')})
]
});

if (NODE_ENV !== 'production') {
logger.add(new transports.Console({
format: format.simple()
}))
}

module.exports = logger
Empty file added logs/.gitignore
Empty file.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"ip-num": "^1.3.1",
"redis": "^3.0.2",
"redlock": "^4.2.0",
"uuid": "^8.3.2"
"uuid": "^8.3.2",
"winston": "^3.3.3"
}
}
Loading

0 comments on commit 914f1dc

Please sign in to comment.