logging capabilities
This commit is contained in:
parent
3f4c835bd1
commit
f12441b3b3
6 changed files with 47 additions and 18 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -4,3 +4,5 @@ package-lock.json
|
|||
start.sh
|
||||
yarn.lock
|
||||
yarn-error.log
|
||||
combined.log
|
||||
error.log
|
|
@ -17,7 +17,8 @@
|
|||
"node-uri": "^1.1.1",
|
||||
"prom-client": "^14.0.1",
|
||||
"short-id": "^0.1.0-1",
|
||||
"shortid": "^2.2.16"
|
||||
"shortid": "^2.2.16",
|
||||
"winston": "^3.8.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"autoprefixer": "^10.4.0",
|
||||
|
|
|
@ -24,9 +24,6 @@ async function submitForm(e, form) {
|
|||
document.getElementById("shortUrl").innerHTML = response.shortUrl
|
||||
document.getElementById("shortUrl").href = response.shortUrl
|
||||
document.getElementById("longUrl").innerHTML = "Long Url: " + response.longUrl
|
||||
if (response.edit) {
|
||||
console.log("Already have this path, editing.")
|
||||
}
|
||||
if (response.old) {
|
||||
document.getElementById("edit").style.visibility = "visible"
|
||||
document.getElementById("oldLongUrl").innerHTML = response.old.longUrl
|
||||
|
|
23
src/main.js
23
src/main.js
|
@ -4,6 +4,29 @@ const path = require('path');
|
|||
const url = require('url')
|
||||
const app = express();
|
||||
const client = require('prom-client');
|
||||
const {format} = require('winston');
|
||||
const winston = require('winston')
|
||||
|
||||
const logger = winston.createLogger({
|
||||
level: 'info',
|
||||
format: format.combine(
|
||||
format.timestamp({
|
||||
format: 'YYYY-MM-DD HH:mm:ss'
|
||||
}),
|
||||
winston.format.json()
|
||||
),
|
||||
defaultMeta: { service: 'user-service' },
|
||||
transports: [
|
||||
//
|
||||
// - Write all logs with importance level of `error` or less to `error.log`
|
||||
// - Write all logs with importance level of `info` or less to `combined.log`
|
||||
//
|
||||
new winston.transports.File({ filename: 'error.log', level: 'error' }),
|
||||
new winston.transports.File({ filename: 'combined.log' }),
|
||||
],
|
||||
});
|
||||
|
||||
module.exports.logger = logger;
|
||||
|
||||
const collectDefaultMetrics = client.collectDefaultMetrics;
|
||||
const register = new client.Registry();
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
const express = require('express')
|
||||
const path = require('path');
|
||||
|
||||
const main = require('../main');
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
const Url = require('../models/url')
|
||||
|
@ -17,10 +19,10 @@ router.get('/:code', async (req, res) => {
|
|||
})
|
||||
if (url) {
|
||||
// when valid we perform a redirect
|
||||
console.log("Redirecting to: " + url.longUrl);
|
||||
main.logger.info("Redirecting to: " + url.longUrl)
|
||||
return res.redirect(url.longUrl)
|
||||
} else {
|
||||
console.log("Redirecting to Home.")
|
||||
main.logger.info("Redirecting to Home.")
|
||||
return res.sendFile(path.join(__dirname + `../../../public/index.html`))
|
||||
// else return a not found 404 status
|
||||
//return res.status(404).json('No URL Found')
|
||||
|
@ -29,7 +31,7 @@ router.get('/:code', async (req, res) => {
|
|||
}
|
||||
// exception handler
|
||||
catch (err) {
|
||||
console.error(err)
|
||||
main.logger.error(err)
|
||||
res.status(500).json('Server Error')
|
||||
}
|
||||
})
|
||||
|
|
|
@ -4,6 +4,8 @@ const express = require('express')
|
|||
const uri = require('node-uri')
|
||||
const shortid = require('shortid')
|
||||
|
||||
const main = require('../main')
|
||||
|
||||
// creating express route handler
|
||||
const router = express.Router()
|
||||
|
||||
|
@ -40,11 +42,11 @@ router.post('/shorten', async (req, res) => {
|
|||
longUrl, customPath
|
||||
} = req.body // destructure the longUrl from req.body.longUrl
|
||||
|
||||
console.log(longUrl, customPath)
|
||||
// check base url if valid using the validUrl method
|
||||
main.logger.info('info', longUrl + " " + customPath)
|
||||
|
||||
// check base url if valid using the validUrl method
|
||||
if (!validUrl(baseUrl)) {
|
||||
console.log("Invalid url " + baseUrl)
|
||||
main.logger.log("info", "invalid base url + baseUrl")
|
||||
return res.status(401).json('Invalid base URL')
|
||||
}
|
||||
|
||||
|
@ -65,7 +67,7 @@ router.post('/shorten', async (req, res) => {
|
|||
// url exist and return the response
|
||||
if (url) {
|
||||
let json = url.toJSON()
|
||||
console.log("Return " + JSON.stringify(json))
|
||||
main.logger.log('info', "url exists, returns it " + JSON.stringify(json))
|
||||
res.json(json)
|
||||
} else {
|
||||
try {
|
||||
|
@ -74,8 +76,6 @@ router.post('/shorten', async (req, res) => {
|
|||
})
|
||||
if (doc) {
|
||||
let json
|
||||
//json["edit"] = true
|
||||
console.log("Found custom path")
|
||||
|
||||
const shortUrl = baseUrl + '/' + customPath
|
||||
|
||||
|
@ -94,9 +94,10 @@ router.post('/shorten', async (req, res) => {
|
|||
|
||||
json["old"] = old.toJSON()
|
||||
|
||||
main.logger.log('info', "using custom path, short url exists, changing old short " + JSON.stringify(json))
|
||||
|
||||
res.json(json)
|
||||
} else {
|
||||
console.log("Not found custom path")
|
||||
// join the generated short code the the base url
|
||||
const shortUrl = baseUrl + '/' + customPath
|
||||
|
||||
|
@ -107,22 +108,25 @@ router.post('/shorten', async (req, res) => {
|
|||
urlCode: customPath,
|
||||
date: new Date()
|
||||
})
|
||||
|
||||
await url.save()
|
||||
console.log("Return " + url)
|
||||
main.logger.log('info', "using custom path, short url do not exists, applying it " + JSON.stringify(url.toJSON()))
|
||||
|
||||
res.json(url)
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
main.logger.error(err)
|
||||
res.status(500).json('Server Error')
|
||||
}
|
||||
}
|
||||
}
|
||||
// exception handler
|
||||
catch (err) {
|
||||
console.log(err)
|
||||
main.logger.error(err)
|
||||
res.status(500).json('Server Error')
|
||||
}
|
||||
} else {
|
||||
main.logger.log("Invalid longUrl " + longUrl)
|
||||
res.status(401).json('Invalid longUrl')
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue