From be2835886bdc8519aac1e917d96a9bc1c513cbf9 Mon Sep 17 00:00:00 2001 From: Peter Olsar Date: Fri, 14 Dec 2018 17:14:04 -0800 Subject: - Added support to specify logger config file on command line. - Added support to specify log directory path in logger config. --- README.md | 20 ++++++++++++++------ logger-config.json | 5 +++-- package.json | 3 ++- services/data-logger.js | 31 ++++++++++++++++++++++++------- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 6610a98..45fa501 100644 --- a/README.md +++ b/README.md @@ -42,29 +42,37 @@ $ npm start ``` # Logging -By default this app will log the current power usage of each plug every minute, and store 24 hours worth of entries (removing the older entries as new ones are added). This log interval and max retention limit are configurable in the `logger-config.json` file in the root project directory. +By default this app will log the current power usage of each plug every minute, and store 24 hours worth of entries (removing the older entries as new ones are added). This log interval and max retention limit are configurable in the `logger-config.json` file in the root project directory. ``` { + // Directory path where to write log files. It will be created if it doesn't already exist. + "logDirPath": "path/to/logs", + // The number of seconds between each log entry - "logIntervalSeconds": 60, - + "logIntervalSeconds": 60, + // The maximum number of log entries to store "maxLogEntries": 1440 // 24hrs at 1 log/min } ``` The logged data is shown on the 'Logged Usage' graph on the dashboard. -Logs are written in JSON format to the project root directory, with the filename `-log.json` e.g. `8FCA808B79-log.json`. Each file contains all the log entries for that plug, up to the maximum configured number, at which point it will remove the oldest entry when adding a new one. +Logs are written in JSON format, with the filename `-log.json` e.g. `8FCA808B79-log.json`. Each file contains all the log entries for that plug, up to the maximum configured number, at which point it will remove the oldest entry when adding a new one. + +You can specify path to a custom logger config file as the first command-line argument, e.g. +``` +npm start /home/username/tplink-logger-config.json +``` If you are running the app from the Docker image and you want to change the logger config, you can mount your desired config file into `/opt/tplink-monitor/`. The logs can be accessed in the same way. -Each logfile is a JSON array of entries. Each entry contains a timestamp in unix/epoch format `ts`, and a power reading in watts `pw`. +Each logfile is a JSON array of entries. Each entry contains a timestamp in unix/epoch format `ts`, and a power reading in watts `pw`. If you want to analyse the log files in Excel or similar office tools you can convert the JSON file into csv format. This can be done numerous ways including online converters such as [konklone.io/json](https://konklone.io/json/), or if you are on a Unix system (or otherwise have access to `sed`) user [ballachango](https://github.com/jamesbarnett91/tplink-energy-monitor/issues/6#issuecomment-433663873) has posted this sed command `sed -e 's/},{/\\\n/g' -e 's/[]["tspw:}{\\]//g' > log.csv` # Note Because the server needs access to your local network to scan for TP-Link device, you must run the server on the same network which your TP Link plugs are connected to. For the vast majority of people this shouldn't be an issue, and you can still use different network interfaces (i.e. plug(s) on WiFi and server on ethernet) as long as they all connect to the same network. -A note for Windows users: There seems to be an issue with the UDP broadcast the server performs to scan for devices which occurs when you also have VirtualBox installed on your Windows machine. I think this is because the response from the plug is routed to the VirtualBox Host-Only network adapter, rather than your primary network interface (for some reason). +A note for Windows users: There seems to be an issue with the UDP broadcast the server performs to scan for devices which occurs when you also have VirtualBox installed on your Windows machine. I think this is because the response from the plug is routed to the VirtualBox Host-Only network adapter, rather than your primary network interface (for some reason). If you hit this issue you can try disabling the VirtualBox adapter in `Control Panel > Network and Internet > Network Connections` and see if that solves the problem. diff --git a/logger-config.json b/logger-config.json index 4746469..d44ae5a 100644 --- a/logger-config.json +++ b/logger-config.json @@ -1,4 +1,5 @@ { - "logIntervalSeconds": 60, + "logDirPath": ".", + "logIntervalSeconds": 60, "maxLogEntries": 1440 -} \ No newline at end of file +} diff --git a/package.json b/package.json index 05ad6c6..43e0f7c 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "morgan": "~1.9.0", "tplink-smarthome-api": "0.22.0", "moment": "2.22.0", - "express-ws": "3.0.0" + "express-ws": "3.0.0", + "shelljs": "0.8.3" }, "devDependencies": { "pkg": "4.3.1" diff --git a/services/data-logger.js b/services/data-logger.js index 9c23de1..f4f4865 100644 --- a/services/data-logger.js +++ b/services/data-logger.js @@ -1,6 +1,9 @@ const fs = require('fs'); +const path = require('path'); +const shell = require('shelljs'); const dataBroadcaster = require('./data-broadcaster'); +let logDirPath; let logIntervalMs; let maxLogEntries; @@ -8,16 +11,30 @@ loadLogConfig(); function loadLogConfig() { try { - let config = JSON.parse(fs.readFileSync('logger-config.json', 'utf8')); + // Use logger config file specified on command line, otherwise use default one. + if (process.argv.length > 2) { + loggerConfigPath = process.argv[2]; + } else { + loggerConfigPath = 'logger-config.json'; + } + console.log('Logger config file: "' + loggerConfigPath + '"') + + let config = JSON.parse(fs.readFileSync(loggerConfigPath, 'utf8')); + logDirPath = config.logDirPath; logIntervalMs = (config.logIntervalSeconds * 1000); maxLogEntries = config.maxLogEntries; } catch (err) { console.warn('Error reading logger config. Reverting to defaults.', err); + logDirPath = '.' // Current directory logIntervalMs = 60000 // 1 min maxLogEntries = 1440 // 24 hrs at 1/min - } + } + + // Create log directory path if it doesn't already exist. + console.log('Log directory path: "' + logDirPath + '"') + shell.mkdir('-p', logDirPath); } function startLogging(device) { @@ -26,7 +43,7 @@ function startLogging(device) { } function writeLog(filePath, log) { - fs.writeFile(filePath, JSON.stringify(log), { flag: 'w' }, (err) => { + fs.writeFileSync(filePath, JSON.stringify(log), { flag: 'w' }, (err) => { if (err) { console.warn('Error writing log for ' + device.alias + ' [' + device.deviceId + ']', err); } @@ -34,7 +51,7 @@ function writeLog(filePath, log) { } function getLogEntries(filePath, callback) { - + fs.access(filePath, fs.constants.F_OK, (err) => { if(err) { // No log file, init empty one @@ -68,7 +85,7 @@ function log(device) { getLogEntries(filePath, (entries) => { entries.push(logEntry) - + // Remove old entries entries.splice(0, entries.length - maxLogEntries); @@ -80,7 +97,7 @@ function log(device) { } function getLogPath(deviceId) { - return deviceId + '-log.json'; + return path.join(logDirPath, deviceId + '-log.json'); } function getLogEntriesForDevice(deviceId, callback) { @@ -91,4 +108,4 @@ module.exports = { startLogging: startLogging, log: log, getLogEntriesForDevice: getLogEntriesForDevice -} \ No newline at end of file +} -- cgit v1.2.3