diff --git a/JavaScript/9-logger/api/user.js b/JavaScript/9-logger/api/user.js index 0e9a8bb..72e59e1 100644 --- a/JavaScript/9-logger/api/user.js +++ b/JavaScript/9-logger/api/user.js @@ -1,5 +1,5 @@ 'use strict'; -const {db, common} = require('../api.di.container'); +const {db, common, console} = require('../api.di.container'); module.exports = { async read(id) { @@ -7,6 +7,7 @@ module.exports = { }, async create({login, password}) { + console.log({login}); const passwordHash = await common.hash(password); return await db('users').create({login, password: passwordHash}); }, @@ -24,4 +25,4 @@ module.exports = { const sql = 'SELECT login from users where login like $1'; return await db('users').query(sql, [mask]); }, -}; \ No newline at end of file +}; diff --git a/JavaScript/9-logger/logger.js b/JavaScript/9-logger/custom-logger.js similarity index 97% rename from JavaScript/9-logger/logger.js rename to JavaScript/9-logger/custom-logger.js index 7f8ad8a..6c4b2af 100644 --- a/JavaScript/9-logger/logger.js +++ b/JavaScript/9-logger/custom-logger.js @@ -68,4 +68,4 @@ class Logger { } } -module.exports = new Logger('./log'); \ No newline at end of file +module.exports = new Logger('./log'); diff --git a/JavaScript/9-logger/logger.di.container.js b/JavaScript/9-logger/logger.di.container.js index 4b276e2..020ec7d 100644 --- a/JavaScript/9-logger/logger.di.container.js +++ b/JavaScript/9-logger/logger.di.container.js @@ -1 +1 @@ -module.exports = Object.freeze(require('./logger')); \ No newline at end of file +module.exports = Object.freeze(require('./pino-logger')); diff --git a/JavaScript/9-logger/main.js b/JavaScript/9-logger/main.js index 749c9e7..53d32cd 100644 --- a/JavaScript/9-logger/main.js +++ b/JavaScript/9-logger/main.js @@ -6,16 +6,7 @@ const config = require('./config.js'); const server = require(`./${config.apiServer.transport}.js`); const staticServer = require('./static.js'); const load = require('./load.js'); -const db = require('./db.js'); -const hash = require('./hash.js'); -const logger = require('./logger.js'); - -const sandbox = { - console: Object.freeze(logger), - db: Object.freeze(db), - common: {hash}, -}; const apiPath = path.join(process.cwd(), './api'); const routing = {}; @@ -25,9 +16,9 @@ const routing = {}; if (!fileName.endsWith('.js')) continue; const filePath = path.join(apiPath, fileName); const serviceName = path.basename(fileName, '.js'); - routing[serviceName] = await load(filePath, sandbox); + routing[serviceName] = require(filePath); } staticServer('./static', config.staticServer.port); server(routing, config.apiServer.port); -})(); \ No newline at end of file +})(); diff --git a/JavaScript/9-logger/pino-logger.js b/JavaScript/9-logger/pino-logger.js new file mode 100644 index 0000000..e3abf41 --- /dev/null +++ b/JavaScript/9-logger/pino-logger.js @@ -0,0 +1,49 @@ +'use strict'; + +const pino = require('pino'); +const util = require('node:util'); +const path = require('node:path'); +const fs = require("node:fs"); + +class Logger { + constructor(logPath) { + const date = new Date().toISOString().substring(0, 10); + const filePath = path.join(logPath, `${date}.log`); + this.logger = pino.pino(pino.destination(filePath)); + } + + close() { + } + + log(...args) { + const msg = util.format(...args); + this.logger.info(msg); + } + + dir(...args) { + const msg = util.inspect(...args); + this.logger.info(msg); + } + + debug(...args) { + const msg = util.format(...args); + this.logger.debug(msg); + } + + error(...args) { + const msg = util.format(...args).replace(/[\n\r]{2,}/g, '\n'); + this.logger.error('error', msg.replace(this.regexp, '')); + } + + system(...args) { + const msg = util.format(...args); + this.logger.fatal(msg); + } + + access(...args) { + const msg = util.format(...args); + this.logger.fatal(msg); + } +} + +module.exports = new Logger('./log'); diff --git a/JavaScript/9-logger/static/client.js b/JavaScript/9-logger/static/client.js index 72e5f17..4e48797 100644 --- a/JavaScript/9-logger/static/client.js +++ b/JavaScript/9-logger/static/client.js @@ -47,7 +47,7 @@ const httpScaffold = (url, structure) => { return api; }; -const wssScaffold = (url, structure) => { +const wsScaffold = (url, structure) => { const api = {}; const services = Object.keys(structure); for (const serviceName of services) { @@ -71,11 +71,11 @@ const wssScaffold = (url, structure) => { const scaffold = (url, structure) => { const protocol = new URL(url).protocol; if (protocol === 'http:') return httpScaffold(url, structure); - if (protocol === 'ws:') return wssScaffold(url, structure); + if (protocol === 'ws:') return wsScaffold(url, structure); throw new Error(`Unknown protocol ${protocol}`); }; -const api = scaffold('ws://localhost:3000', { +const api = scaffold('http://localhost:3000', { user: { create: ['record'], read: ['id'], @@ -91,7 +91,7 @@ const api = scaffold('ws://localhost:3000', { }); (async () => { - const [{id}] = await api.user.create({login: 'rand1', password: 'test'}); + const [{id}] = await api.user.create({login: (Math.random() + 1).toString().substring(1), password: 'test'}); const [user] = await api.user.read(id); console.log(user); -})(); \ No newline at end of file +})();