From 4cd20f0fa8c040989e8b9e8037c00bbafeb9b653 Mon Sep 17 00:00:00 2001 From: Chad Scharf <3904944+cscharf@users.noreply.github.com> Date: Tue, 20 Oct 2020 09:33:30 -0400 Subject: [PATCH] Add logging to lowdb storage service (#188) --- src/services/lowdbStorage.service.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/services/lowdbStorage.service.ts b/src/services/lowdbStorage.service.ts index ae9865c1a7..aa69717a19 100644 --- a/src/services/lowdbStorage.service.ts +++ b/src/services/lowdbStorage.service.ts @@ -3,6 +3,7 @@ import * as lowdb from 'lowdb'; import * as FileSync from 'lowdb/adapters/FileSync'; import * as path from 'path'; +import { LogService } from '../abstractions/log.service'; import { StorageService } from '../abstractions/storage.service'; import { NodeUtils } from '../misc/nodeUtils'; @@ -13,28 +14,37 @@ export class LowdbStorageService implements StorageService { private defaults: any; private dataFilePath: string; - constructor(defaults?: any, dir?: string, private allowCache = false) { + constructor(private logService: LogService, defaults?: any, dir?: string, private allowCache = false) { this.defaults = defaults; + this.logService.info('Initializing lowdb storage service.'); let adapter: lowdb.AdapterSync; if (Utils.isNode && dir != null) { if (!fs.existsSync(dir)) { + this.logService.warning(`Could not find dir, "${dir}"; creating it instead.`); NodeUtils.mkdirpSync(dir, '700'); + this.logService.info(`Created dir "${dir}".`); } this.dataFilePath = path.join(dir, 'data.json'); if (!fs.existsSync(this.dataFilePath)) { + this.logService.warning(`Could not find data file, "${this.dataFilePath}"; creating it instead.`); fs.writeFileSync(this.dataFilePath, '', { mode: 0o600 }); fs.chmodSync(this.dataFilePath, 0o600); + this.logService.info(`Created data file "${this.dataFilePath}" with chmod 600.`); } adapter = new FileSync(this.dataFilePath); } try { + this.logService.info('Attempting to create lowdb storage adapter.'); this.db = lowdb(adapter); + this.logService.info('Successfuly created lowdb storage adapter.'); } catch (e) { if (e instanceof SyntaxError) { + this.logService.warning(`Error creating lowdb storage adapter, "${e.message}"; emptying data file.`); adapter.write({}); this.db = lowdb(adapter); } else { + this.logService.error(`Error creating lowdb storage adapter, "${e.message}".`); throw e; } } @@ -42,8 +52,10 @@ export class LowdbStorageService implements StorageService { init() { if (this.defaults != null) { + this.logService.info('Writing defaults.'); this.readForNoCache(); this.db.defaults(this.defaults).write(); + this.logService.info('Successfully wrote defaults to db.'); } }