Add logging

This commit is contained in:
Evgenii Abramov
2021-05-16 23:53:55 +03:00
parent 4d2f87be6a
commit 07bff61978
5 changed files with 45 additions and 27 deletions

View File

@@ -1,5 +1,6 @@
'use strict'; 'use strict';
const {logger, authl} = global;
const loki = require('lokijs'); const loki = require('lokijs');
global.dbl = new loki('./loki.json', { global.dbl = new loki('./loki.json', {
@@ -7,46 +8,50 @@ global.dbl = new loki('./loki.json', {
autosave: true, autosave: true,
autosaveInterval: 5000, autosaveInterval: 5000,
autoloadCallback() { autoloadCallback() {
global.authl = global.dbl.getCollection('tokens'); authl = global.dbl.getCollection('tokens');
if (global.authl === null) { if (authl === null) {
global.authl = global.dbl.addCollection('tokens'); authl = global.dbl.addCollection('tokens');
} }
} }
}); });
module.exports.find = (key, done) => { module.exports.find = (key, done) => {
const ltoken = global.authl.findOne({'token': key}); const ltoken = authl.findOne({'token': key});
if (ltoken){ if (ltoken){
const {userId, clientId} = ltoken; const {userId, clientId} = ltoken;
return done(null, {userId, clientId}) return done(null, {userId, clientId})
} else { } else {
global.logger.log('error', new Error('Token Not Found')); logger.log('error', new Error('Token Not Found'));
return done(); return done();
} }
}; };
module.exports.findByUserIdAndClientId = (userId, clientId, done) => { module.exports.findByUserIdAndClientId = (userId, clientId, done) => {
const ltoken = global.authl.findOne({'userId': userId}); const ltoken = authl.findOne({'userId': userId});
if (ltoken){ if (ltoken){
global.logger.log('info', {message: `Load token by userId (${userId}): User found`}); logger.log('info', {message: `Load token by userId (${userId}): User found`});
const {token, userId: uid, clientId: cid} = ltoken; const {token, userId: uid, clientId: cid} = ltoken;
if (uid === userId && cid === clientId) return done(null, token); if (uid === userId && cid === clientId) {
else return done(new Error('Token Not Found')); return done(null, token);
} else {
logger.log('error', new Error('Token Not Found'));
return done();
}
} else { } else {
console.log('User not found'); logger.log('error', new Error('User Not Found'));
return done(new Error('User Not Found')); return done();
} }
}; };
module.exports.save = (token, userId, clientId, done) => { module.exports.save = (token, userId, clientId, done) => {
global.logger.log('info', {message: `Start saving token`}); logger.log('info', {message: `Start saving token`});
const ltoken = global.authl.findOne({'userId': userId}); const ltoken = authl.findOne({'userId': userId});
if (ltoken){ if (ltoken){
global.logger.log('info', {message: `User Updated`}); logger.log('info', {message: `User Updated`});
global.authl.update(Object.assign({}, ltoken, {token, userId, clientId})); authl.update(Object.assign({}, ltoken, {token, userId, clientId}));
} else { } else {
global.logger.log('info', {message: `User not Found. Create new...`}); logger.log('info', {message: `User not Found. Create new...`});
global.authl.insert({'type': 'token', token, userId, clientId}); authl.insert({'type': 'token', token, userId, clientId});
} }
done(); done();
}; };

View File

@@ -1,10 +1,15 @@
'use strict'; 'use strict';
const {logger} = global;
const codes = {}; const codes = {};
module.exports.find = (key, done) => { module.exports.find = (key, done) => {
if (codes[key]) return done(null, codes[key]); if (codes[key]) {
return done(new Error('Code Not Found')); return done(null, codes[key]);
} else {
logger.log('error', new Error('Code Not Found'));
return done();
}
}; };
module.exports.save = (code, clientId, redirectUri, userId, userName, done) => { module.exports.save = (code, clientId, redirectUri, userId, userName, done) => {

View File

@@ -1,17 +1,20 @@
'use strict'; 'use strict';
const {logger} = global;
const {clients} = require('../config'); const {clients} = require('../config');
module.exports.findById = (id, done) => { module.exports.findById = (id, done) => {
for (const client of clients) { for (const client of clients) {
if (client.id === id) return done(null, client); if (client.id === id) return done(null, client);
} }
return done(new Error('Client Not Found')); logger.log('error', new Error('Client Not Found'));
return done();
}; };
module.exports.findByClientId = (clientId, done) => { module.exports.findByClientId = (clientId, done) => {
for (const client of clients) { for (const client of clients) {
if (client.clientId === clientId) return done(null, client); if (client.clientId === clientId) return done(null, client);
} }
return done(new Error('Client Not Found')); logger.log('error', new Error('Client Not Found'));
return done();
}; };

View File

@@ -1,17 +1,20 @@
'use strict'; 'use strict';
const {logger} = global;
const {users} = require('../config'); const {users} = require('../config');
module.exports.findById = (id, done) => { module.exports.findById = (id, done) => {
for (const user of users) { for (const user of users) {
if (user.id === id) return done(null, user); if (user.id === id) return done(null, user);
} }
return done(new Error('User Not Found')); logger.log('error', new Error('User Not Found'));
return done();
}; };
module.exports.findByUsername = (username, done) => { module.exports.findByUsername = (username, done) => {
for (const user of users) { for (const user of users) {
if (user.username === username) return done(null, user); if (user.username === username) return done(null, user);
} }
return done(new Error('User Not Found')); logger.log('error', new Error('User Not Found'));
return done();
}; };

View File

@@ -1,3 +1,5 @@
const {logger} = global;
/* function for convert system values to Yandex (depends of capability or property type) */ /* function for convert system values to Yandex (depends of capability or property type) */
function convertToYandexValue(val, actType) { function convertToYandexValue(val, actType) {
switch(actType) { switch(actType) {
@@ -8,7 +10,7 @@ function convertToYandexValue(val, actType) {
const value = parseFloat(val); const value = parseFloat(val);
return isNaN(value) ? 0.0 : value; return isNaN(value) ? 0.0 : value;
} catch(e) { } catch(e) {
console.error(`Can't parse to float: ${val}`); logger.log('error', {message: `Can't parse to float: ${val}`});
return 0.0; return 0.0;
} }
} }
@@ -96,7 +98,7 @@ class Device {
} }
} }
default: { default: {
console.error(`Unsupported capability type: ${type}`) logger.log('error', {message: `Unsupported capability type: ${type}`});
return undefined; return undefined;
} }
} }
@@ -195,7 +197,7 @@ class Device {
message = `${value}`; message = `${value}`;
} catch(e) { } catch(e) {
topic = false; topic = false;
global.logger.log('error', {message: `${e}`}); logger.log('error', {message: `${e}`});
} }
if (topic) { if (topic) {
@@ -225,7 +227,7 @@ class Device {
const value = this.getMappedValue(val, actType, false); const value = this.getMappedValue(val, actType, false);
cp.state = {instance, value: convertToYandexValue(value, actType)}; cp.state = {instance, value: convertToYandexValue(value, actType)};
} catch(e) { } catch(e) {
global.logger.log('error', {message: `${e}`}); logger.log('error', {message: `${e}`});
} }
} }
} }