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