@egi/smart-db
Version:
Unified Smart DB Access
1 lines • 4.18 kB
JavaScript
import _ from"lodash";import{SmartDbLogModel}from"./models/smart-db-log-model";import{SmartErrorSeverity,SmartErrorLocation}from"./smart-error";import{SmartDbDateRegexp,SmartDbTimestampRegexp,toSmartDbTimestamp}from"./smart-db-globals";export const SeverityLevel={None:0,Fatal:1,Error:2,Warning:3,Info:4,Debug:5,All:9};export class SmartDbLog{constructor(t){this.userId=null,this.dbLogging=!0,this.isLogging=!1,this.logFifo=[],this.db=t,this.dbLogging=!1,this.dbLogLevel=SeverityLevel.Info,this.consoleLogLevel=SeverityLevel.Info}getConsoleLogLevel(){return this.consoleLogLevel}getDbLogLevel(){return this.dbLogLevel}getDbLogging(){return this.dbLogging}logDebug(t,e,r,o){this.writeLog(t,e,SmartErrorSeverity.Debug,r,o)}logError(t,e,r,o){this.writeLog(t,e,SmartErrorSeverity.Error,r,o)}logFatal(t,e,r,o){this.writeLog(t,e,SmartErrorSeverity.Fatal,r,o)}logInfo(t,e,r,o){this.writeLog(t,e,SmartErrorSeverity.Info,r,o)}logWarning(t,e,r,o){this.writeLog(t,e,SmartErrorSeverity.Warning,r,o)}setConsoleLogLevel(t){this.consoleLogLevel=t}setDb(t){this.db=t,this.db?this.db.exists(SmartDbLogModel).then((t=>{this.dbLogging=t})).catch((t=>{throw t})):this.dbLogging=!1}setDbLogLevel(t){this.dbLogLevel=t}setDbLogging(t){this.dbLogging=t}setUserId(t){this.userId=t}writeLog(t,e,r,o,i,s){try{if(this.isLogging)this.logFifo.push({type:t,location:e,severity:r,message:o,data:i,timestamp:s??new Date});else{this.isLogging=!0;let a,g,n=SeverityLevel.None;switch(r){case SmartErrorSeverity.Fatal:a=console.error,n=SeverityLevel.Fatal;break;case SmartErrorSeverity.Error:a=console.error,n=SeverityLevel.Error;break;case SmartErrorSeverity.Warning:a=console.warn,n=SeverityLevel.Warning;break;case SmartErrorSeverity.Info:a=console.info,n=SeverityLevel.Info;break;case SmartErrorSeverity.Debug:a=console.debug,n=SeverityLevel.Debug}if(_.isString(s)){const t=s.match(SmartDbTimestampRegexp),e=t&&s.match(SmartDbDateRegexp);g=t?t[1]:e?e[1]+".000":toSmartDbTimestamp(new Date)}else g=_.isDate(s)?toSmartDbTimestamp(s):_.isNumber(s)?toSmartDbTimestamp(new Date(s)):toSmartDbTimestamp(new Date);if(_.isObjectLike(o))if(o instanceof Error)i||(i=o),o=o.message;else try{o=JSON.stringify(o)}catch(t){o=o.toString()}else"boolean"==typeof o&&(o=o?"<true>":"<false>");if(i){if(i instanceof Error)i=i.stack;else if(_.isObject(i))try{i=JSON.stringify(i)}catch(t){i=i.toString(),this.db&&this.dbLogLevel>=n&&console.error("unable to stringify log data",i)}else i.toString?i=i.toString():(i=null,this.db&&this.dbLogLevel>=n&&console.error("unable to store log data",i));i&&(o+=` (${i})`)}if(this.consoleLogLevel>=n){const s=t.substring(0,1).toUpperCase();if(t==SmartErrorLocation.Database&&this.db&&a(`${r}-${s}-${g}: last statement:`,this.db.getLastBuildData()),a(`${r}-${s}-${g}: ${o}`),this.dbLogging)try{t==SmartErrorLocation.Database&&(i||(i=JSON.stringify(this.db.getLastBuildData()))),this.db.supportSyncCalls?(this.db.insertSync(SmartDbLogModel,{severity:r,type:t,location:e,info:o||"<empty>",data:i,user:this.userId,timestamp:new Date(g)}),this.isLogging=!1,this.nextStackEntry()):this.db.insert(SmartDbLogModel,{severity:r,type:t,location:e,info:o||"<empty>",data:i,user:this.userId,timestamp:new Date(g)}).then((()=>{this.db.commit().then((()=>{this.isLogging=!1,this.nextStackEntry()})).catch((t=>{console.error(`F-B-${g}: unable to commit log transaction`,t,t.code,t.name,t.message),this.isLogging=!1,this.nextStackEntry()}))})).catch((t=>{console.error(`F-B-${g}: unable to write the log statement below to database`,t,t.code,t.name,t.message),this.isLogging=!1,this.nextStackEntry()}))}catch(t){console.error(`F-B-${g}: unable to write the log statement below to database`,t,t.code,t.name,t.message);try{a(`F-B-${g}: last statement:`,this.db.getLastBuildData())}catch{a(`F-B-${g}: last statement not available`)}this.isLogging=!1,this.nextStackEntry()}else this.isLogging=!1,this.nextStackEntry()}else this.isLogging=!1,this.nextStackEntry()}}catch(t){this.isLogging=!1,console.error("fatal logging error",t)}}nextStackEntry(){if(this.logFifo.length>0){const t=this.logFifo.shift();this.writeLog(t.type,t.location,t.severity,t.message,t.data,t.timestamp)}}}export const smartDbLog=new SmartDbLog;