Skip to content

Commit

Permalink
[1.0.1] - Add browser methods
Browse files Browse the repository at this point in the history
  • Loading branch information
shbatm committed Jan 7, 2019
1 parent 2691d17 commit f3b6dd7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [1.0.1] - Add browser methods

## [1.0.0] - Unreleased initial commit.
#### Added:
- `overwriteBrowserMethods` option to format the Web Browser's (DevTools) logs as well as the Node.JS console logs. Defaults to `false` so to enable it, you must add it in your config section.
- `echoModuleNotifications` option to relay module notifications to the NodeJS console as well as DevTools.
- `echoErrors` option to relay browser errors to the NodeJS console as well as DevTools.

## [1.0.0] - Initial Release

* Initial commit for public testing.
43 changes: 40 additions & 3 deletions MMM-Logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,50 @@
* MIT Licensed.
*/

/* jshint esversion: 6 */

Module.register("MMM-Logging", {
defaults: {
useColor: true,
format: "{{timestamp}} <{{title}}> {{message}} ({{folder}}/{{file}}:{{line}} {{method}})",
overwriteConsoleMethods: true,
overwriteBrowserMethods: false,
echoModuleNotifications: 'notification',
echoErrors: true,
dateformat: "yyyy-mm-dd'T'HH:MM:ss",
},

requiresVersion: "2.1.0", // Required version of MagicMirror

start: function() {
var self = this;
this.sendSocketNotification("INITIALIZE_LOGGING", this.config);
if (this.config.overwriteBrowserMethods) {
this.config.overwriteConsoleMethods = true;
// Overwrite the Main Console
this.console = Tracer.console(this.config);
// Overwrite MagicMirror's Log functions.
Log.log = console.log;
Log.info = console.info;
Log.warn = console.warn;
Log.error = console.error;
Log.debug = (console.debug || console.log);
}
console.info("MMM-Logging updated window.console.");

if (this.config.echoErrors) {
console.error = (text) => {
this.sendSocketNotification("BROWSER_ERROR", text);
this.console.error(text);
};
window.addEventListener('error', (event) => {
this.sendSocketNotification("BROWSER_ERROR", event);
});
}
this.initialized = true;
},

getScripts: function() {
return [];
return ['tracer-bundle.js'];
},

getStyles: function() {
Expand All @@ -36,6 +63,16 @@ Module.register("MMM-Logging", {
},

notificationReceived: function(notification, payload, sender) {
// Do nothing.
if (this.config.echoModuleNotifications) {
this.sendSocketNotification("NOTIFICATION_TO_CONSOLE", {
notification: notification,
payload: (payload && this.config.echoModuleNotifications === "payload") ? payload : undefined,
sender: (sender) ? sender.nam : undefined
});
}
if (notification === "CALENDAR_EVENTS") {
testerror();
}
},

});
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Refer to documentation for [`tracer`](https://github.com/baryon/tracer) for most
| Option | Description
|----------------- |-----------
| `useColor` | *Optional* Whether or not to use `tracer`'s `colorConsole` method or regular `console` method. <br>**Type:** `bool` *Default* `true`.
| `overwriteConsoleMethods` | *Optional* Whether or not to overwrite the default Node.JS console methods. If `true` any `console.log`, `console.error`, etc. function calls in any module will be formatted. If `false`, only calls to `tracer.log`, etc. will be formatted. In most cases for MagicMirror, if you're using this module, you want `true` <br>**Type:** `bool` *Default* `true`.
| `overwriteBrowserMethods` | *Optional* Whether or not to overwrite the default web browser console methods. If `true` any `console.log`, `console.error`, etc. function calls *in the DevTools console* in any module will be formatted. If `false`, only calls to `tracer.log`, etc. will be formatted.<br>**Type:** `bool` *Default* `false`--usually the DevTools console is good enough for tracing errors.
| `echoModuleNotifications` | *Optional* If set any module notifications sent on the front-end will be printed on the Node.JS console log. Can be set to 'notification' to just send the notifications, or 'payload' to include the payloads as well.<br>**Type:** `string` *Default* `'notification'`.
| `echoErrors` | *Optional* If `true`, any errors in the web browser (front-end) will be printed on the Node.JS console log. <br>**Type:** `bool` *Default* `true`.

#### Default configuration:

Expand Down
12 changes: 9 additions & 3 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/* jshint node: true, esversion: 6*/

var NodeHelper = require("node_helper");
var newConsole = require("tracer").console({
var tracer = require("tracer").console({
format: "{{timestamp}} <{{title}}> {{message}} ({{folder}}/{{file}}:{{line}} {{method}})",
overwriteConsoleMethods: true,
dateformat: "yyyy-mm-dd'T'HH:MM:ss",
Expand All @@ -23,12 +23,18 @@ module.exports = NodeHelper.create({
if (notification === "INITIALIZE_LOGGING" && !this.initialized) {
this.config = payload;
if (this.config.useColor) {
newConsole = require('tracer').colorConsole(this.config);
tracer = require('tracer').colorConsole(this.config);
} else {
newConsole = require('tracer').console(this.config);
tracer = require('tracer').console(this.config);
}
console.info("MMM-Logging updated config received, reloading console");
this.initialized = true;
}
if (notification === "NOTIFICATION_TO_CONSOLE") {
tracer.log("Module Notification: " + payload.notification + ((payload.sender) ? " from " + payload.sender : "") + (payload.payload) ? " payload: " + JSON.stringify(payload.payload, undefined, 3) : "");
}
if (notification === "BROWSER_ERROR") {
tracer.log("Browser Error: " + JSON.stringify(payload, undefined, 3));
}
},
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"homepage": "https://github.com/shbatm/MMM-Logging#readme",
"dependencies": {
"tinyify": "^2.5.0",
"tracer": "github:shbatm/tracer"
}
}
2 changes: 2 additions & 0 deletions tracer-bundle.js

Large diffs are not rendered by default.

0 comments on commit f3b6dd7

Please sign in to comment.