REST Service for POPCORN - ILIAS
alex
7 hours ago a82027a51a3056e574b9c6c8941a7a16f5841126
GS-2555
5 files modified
1 files added
57 ■■■■■ changed files
.mocharc.js 7 ●●●●● patch | view | raw | blame | history
README.md 10 ●●●●● patch | view | raw | blame | history
lib/libIlias.js 7 ●●●●● patch | view | raw | blame | history
logger.js 27 ●●●●● patch | view | raw | blame | history
settings.default.json 3 ●●●●● patch | view | raw | blame | history
settings.js 3 ●●●●● patch | view | raw | blame | history
.mocharc.js
New file
@@ -0,0 +1,7 @@
// Mocha config: keep manual test runs quiet unless LOG_LEVEL is set explicitly.
process.env.LOG_LEVEL ||= "error"
module.exports = {
   exit: true,
   timeout: 20000,
}
README.md
@@ -232,6 +232,16 @@
### Logs
The log level is configured in `settings.$NODE_ENV.json` via `log.level`
(default `info`). The `LOG_LEVEL` environment variable overrides it, which is
handy for quiet test runs:
    LOG_LEVEL=error npm test
Valid levels: `debug`, `info`, `warn`, `error`, and `silent` (or `off`) to
disable logging entirely. Messages below the active level are neither printed
nor written to `log.log`.
To prevent logs from growing too big the file `$INSTALLDIR/log.log` should be included into a logrotation job.
Furthermore `pm2` also does some logging.
lib/libIlias.js
@@ -5,6 +5,7 @@
const db = require("./db")
const {url, iliastoken} = settings.ilias
const search = require("./search")
const log = require("../logger")
/////////////////////////////////////////////////////////////////////////
@@ -35,7 +36,7 @@
      token: iliastoken,
   })
   let url2 = `${url}?${sp.toString()}`
   console.log("pinging url", url2)
   log.debug("pinging url", url2)
   const res = await fetch(url2, {method: "GET"})
   return await res.json() //
}
@@ -192,7 +193,7 @@
      token: iliastoken,
   })
   let url2 = `${url}?${sp.toString()}`
   console.log(url2)
   log.debug(url2)
   const res = await fetch(url2, {method: "POST"})
   return await res.json()
}
@@ -205,7 +206,7 @@
      token: iliastoken,
   })
   let url2 = `${url}?${sp.toString()}`
   console.log(url2)
   log.debug(url2)
   const res = await fetch(url2, {method: "DELETE"})
   return await res.json()
}
logger.js
@@ -5,16 +5,43 @@
/////////////////////////////////////////////////////////////////////////
const LEVELS = {debug: 10, info: 20, warn: 30, error: 40}
const DEFAULT_LEVEL = "info"
// level configured from settings (see settings.js); env LOG_LEVEL overrides it
let settingsLevel = null
module.exports = {
   debug: msg => log("DEBUG", msg),
   info: msg => log("INFO", msg),
   warn: msg => log("WARN", msg),
   error: msg => log("ERROR", msg),
   setLevel,
   level: currentLevel,
}
/////////////////////////////////////////////////////////////////////////
/**
 * Set the level from settings. The LOG_LEVEL env var still takes precedence.
 */
function setLevel (level) {
   settingsLevel = level ? level.toString().trim().toLowerCase() : null
}
/**
 * Resolve the active log level: LOG_LEVEL env var > settings level > "info".
 */
function currentLevel () {
   const level = process.env.LOG_LEVEL || settingsLevel || DEFAULT_LEVEL
   return level.toString().trim().toLowerCase()
}
function log (level, msg) {
   const threshold = currentLevel()
   if (threshold === "silent" || threshold === "off") return
   if (LEVELS[level.toLowerCase()] < (LEVELS[threshold] ?? LEVELS[DEFAULT_LEVEL])) return
   if (!_.isString(msg)) {
      if (msg.message) {
         msg = msg.message
settings.default.json
@@ -2,6 +2,9 @@
   "authtoken": "jiuGfr432898D90290kjfsldkfn3hh8F",
   "port": 4101,
   "restUrl": "http://localhost:4101",
   "log": {
      "level": "info"
   },
   "db": {
      "host": "localhost",
      "port": 33009,
settings.js
@@ -12,4 +12,7 @@
var settings = nconf.get()
// configure the logger level (LOG_LEVEL env var still takes precedence)
log.setLevel(settings.log && settings.log.level)
module.exports = settings