Permalink
Browse files

Added sentry support, log levels config

1 parent 1399e16 commit 7d75f814f77df002e6a6fac0d84ca3114a633177 @brussell98 committed Jan 11, 2017
Showing with 230 additions and 26 deletions.
  1. +3 −1 README.md
  2. +157 −11 docs/Logger.html
  3. +2 −1 docs/index.html
  4. +1 −0 lib/Bot.js
  5. +64 −11 lib/Logger.js
  6. +3 −2 package.json
View
@@ -4,9 +4,11 @@
A powerful Discord bot core using [Eris](https://github.com/abalabahaha/eris/). At least NodeJS v6 *Argon* is **REQUIRED**
+#### [Documentation](http://brussell.me/Mirai/index.html) | [Eris Docs](https://abal.moe/Eris/docs/index.html) | [Mirai Bot Website](http://mirai.brussell.me) | [Support me on Patreon](http://patreon.com/brussell98) | [Discord Server](https://discord.gg/rkWPSdu) | [Mirai Bot Combined Todo List](https://trello.com/b/Uw5wZLzJ)
+
> By using mirai-bot-core you agree to not ask any questions unless you have a basic understanding of installing programs, running node applications, and developing JavaScript applications.
-#### [Documentation](http://brussell.me/Mirai/index.html) | [Eris Docs](https://abal.moe/Eris/docs/index.html) | [Mirai Bot Website](http://mirai.brussell.me) | [Support me on Patreon](http://patreon.com/brussell98) | [Discord Server](https://discord.gg/rkWPSdu) | [Mirai Bot Combined Todo List](https://trello.com/b/Uw5wZLzJ)
+mirai-bot-core supports advanced application monitoring with **[sentry.io](http://ssentry.io)** using raven. For more information [head over to the docs](http://brussell.me/Mirai/Logger.html).
## Installing:
```
View
@@ -88,14 +88,33 @@ <h1 class="page-title">Logger</h1>
<header>
<h2> Logger </h2>
<div class="class-description">
- <p>Displays text to the console</p>
+ <p>Displays text to the console. Also supports sentry</p>
</div>
</header>
<article>
<div class="container-overview">
<h2>Constructor</h2>
<h4 class="name" id="Logger"><span class="type-signature"></span>new Logger<span class="signature">(options<span class="signature-attributes">opt</span>)</span><span class="type-signature"></span></h4>
<dl class="details"> </dl>
+ <h5 class="subsection-title">Properties:</h5>
+ <table class="props">
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Type</th>
+ <th class="last">Description</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td class="name"><code>raven</code></td>
+ <td class="type"> <span class="param-type">RavenClient</span> </td>
+ <td class="description last">
+ <p>The Raven client used for sentry, if enabled.</p>
+ </td>
+ </tr>
+ </tbody>
+ </table>
<div class="description">
<p>Creates a new Logger</p>
</div>
@@ -129,30 +148,157 @@ <h4 class="name" id="Logger"><span class="type-signature"></span>new Logger<span
</thead>
<tbody>
<tr>
- <td class="name"><code>showDebug</code></td>
+ <td class="name"><code>timestamps</code></td>
<td class="type"> <span class="param-type">Boolean</span> </td>
<td class="attributes"> &lt;optional><br> </td>
<td class="default"> <code>false</code> </td>
<td class="description last">
- <p>Display <code>debug()</code> in the console</p>
+ <p>Add a timestamp to each message logged</p>
</td>
</tr>
<tr>
- <td class="name"><code>onlyErrors</code></td>
- <td class="type"> <span class="param-type">Boolean</span> </td>
+ <td class="name"><code>levels</code></td>
+ <td class="type"> <span class="param-type">Object</span> </td>
<td class="attributes"> &lt;optional><br> </td>
- <td class="default"> <code>false</code> </td>
+ <td class="default"> </td>
<td class="description last">
- <p>Only display <code>error()</code> in the console</p>
+ <p>An object deciding what log levels should display</p>
+ <h6>Properties</h6>
+ <table class="params">
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Type</th>
+ <th>Attributes</th>
+ <th>Default</th>
+ <th class="last">Description</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td class="name"><code>log</code></td>
+ <td class="type"> <span class="param-type">Boolean</span> </td>
+ <td class="attributes"> &lt;optional><br> </td>
+ <td class="default"> <code>true</code> </td>
+ <td class="description last">
+ <p>Show <code>log()</code></p>
+ </td>
+ </tr>
+ <tr>
+ <td class="name"><code>info</code></td>
+ <td class="type"> <span class="param-type">Boolean</span> </td>
+ <td class="attributes"> &lt;optional><br> </td>
+ <td class="default"> <code>true</code> </td>
+ <td class="description last">
+ <p>Show <code>info()</code></p>
+ </td>
+ </tr>
+ <tr>
+ <td class="name"><code>debug</code></td>
+ <td class="type"> <span class="param-type">Boolean</span> </td>
+ <td class="attributes"> &lt;optional><br> </td>
+ <td class="default"> <code>false</code> </td>
+ <td class="description last">
+ <p>Show <code>debug()</code></p>
+ </td>
+ </tr>
+ <tr>
+ <td class="name"><code>warning</code></td>
+ <td class="type"> <span class="param-type">Boolean</span> </td>
+ <td class="attributes"> &lt;optional><br> </td>
+ <td class="default"> <code>true</code> </td>
+ <td class="description last">
+ <p>Show <code>warn()</code></p>
+ </td>
+ </tr>
+ <tr>
+ <td class="name"><code>error</code></td>
+ <td class="type"> <span class="param-type">Boolean</span> </td>
+ <td class="attributes"> &lt;optional><br> </td>
+ <td class="default"> <code>true</code> </td>
+ <td class="description last">
+ <p>Show <code>error()</code></p>
+ </td>
+ </tr>
+ </tbody>
+ </table>
</td>
</tr>
<tr>
- <td class="name"><code>timestamps</code></td>
- <td class="type"> <span class="param-type">Boolean</span> </td>
+ <td class="name"><code>raven</code></td>
+ <td class="type"> <span class="param-type">Object</span> </td>
<td class="attributes"> &lt;optional><br> </td>
- <td class="default"> <code>false</code> </td>
+ <td class="default"> </td>
<td class="description last">
- <p>Add a timestamp to each message logged</p>
+ <p>Raven options. If falsy, raven will not be required.</p>
+ <h6>Properties</h6>
+ <table class="params">
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Type</th>
+ <th>Attributes</th>
+ <th>Default</th>
+ <th class="last">Description</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td class="name"><code>url</code></td>
+ <td class="type"> <span class="param-type">String</span> </td>
+ <td class="attributes"> </td>
+ <td class="default"> </td>
+ <td class="description last">
+ <p>Your project's sentry.io url</p>
+ </td>
+ </tr>
+ <tr>
+ <td class="name"><code>config</code></td>
+ <td class="type"> <span class="param-type">Object</span> </td>
+ <td class="attributes"> &lt;optional><br> </td>
+ <td class="default"> </td>
+ <td class="description last">
+ <p>A custom raven config. For more info check <a href="https://docs.sentry.io/clients/node/config/">their docs</a></p>
+ </td>
+ </tr>
+ <tr>
+ <td class="name"><code>info</code></td>
+ <td class="type"> <span class="param-type">Boolean</span> </td>
+ <td class="attributes"> &lt;optional><br> </td>
+ <td class="default"> <code>false</code> </td>
+ <td class="description last">
+ <p>Send <code>info()</code> messages to sentry</p>
+ </td>
+ </tr>
+ <tr>
+ <td class="name"><code>debug</code></td>
+ <td class="type"> <span class="param-type">Boolean</span> </td>
+ <td class="attributes"> &lt;optional><br> </td>
+ <td class="default"> <code>false</code> </td>
+ <td class="description last">
+ <p>Send <code>debug()</code> messages to sentry</p>
+ </td>
+ </tr>
+ <tr>
+ <td class="name"><code>warning</code></td>
+ <td class="type"> <span class="param-type">Boolean</span> </td>
+ <td class="attributes"> &lt;optional><br> </td>
+ <td class="default"> <code>false</code> </td>
+ <td class="description last">
+ <p>Send <code>warn()</code> messages to sentry. If an <code>Error</code> is passed in the arguments, that will be sent alone.</p>
+ </td>
+ </tr>
+ <tr>
+ <td class="name"><code>error</code></td>
+ <td class="type"> <span class="param-type">Boolean</span> </td>
+ <td class="attributes"> &lt;optional><br> </td>
+ <td class="default"> <code>true</code> </td>
+ <td class="description last">
+ <p>Send <code>error()</code> errors to sentry. If an <code>Error</code> is passed in the arguments, that will be sent alone.</p>
+ </td>
+ </tr>
+ </tbody>
+ </table>
</td>
</tr>
</tbody>
View
@@ -94,10 +94,11 @@
<a href="https://www.npmjs.com/package/mirai-bot-core"><img src="https://img.shields.io/npm/v/mirai-bot-core.svg" alt="npm"></a>
</p>
<p>A powerful Discord bot core using <a href="https://github.com/abalabahaha/eris/">Eris</a>. At least NodeJS v6 <em>Argon</em> is <strong>REQUIRED</strong></p>
+ <h4><a href="http://brussell.me/Mirai/index.html">Documentation</a> | <a href="https://abal.moe/Eris/docs/index.html">Eris Docs</a> | <a href="http://mirai.brussell.me">Mirai Bot Website</a> | <a href="http://patreon.com/brussell98">Support me on Patreon</a> | <a href="https://discord.gg/rkWPSdu">Discord Server</a> | <a href="https://trello.com/b/Uw5wZLzJ">Mirai Bot Combined Todo List</a></h4>
<blockquote>
<p>By using mirai-bot-core you agree to not ask any questions unless you have a basic understanding of installing programs, running node applications, and developing JavaScript applications.</p>
</blockquote>
- <h4><a href="http://brussell.me/Mirai/index.html">Documentation</a> | <a href="https://abal.moe/Eris/docs/index.html">Eris Docs</a> | <a href="http://mirai.brussell.me">Mirai Bot Website</a> | <a href="http://patreon.com/brussell98">Support me on Patreon</a> | <a href="https://discord.gg/rkWPSdu">Discord Server</a> | <a href="https://trello.com/b/Uw5wZLzJ">Mirai Bot Combined Todo List</a></h4>
+ <p>mirai-bot-core supports advanced application monitoring with <strong><a href="http://ssentry.io">sentry.io</a></strong> using raven. For more information <a href="http://brussell98.tk/Mirai/Logger.html">head over to the docs</a>.</p>
<h2>Installing:</h2><pre class="prettyprint source"><code>npm i -S mirai-bot-core</code></pre><pre class="prettyprint source lang-js"><code>const Mirai = require('mirai-bot-core'),
bot = new Mirai(config);</code></pre>
<p>Add your bot to a server using <code>https://discordapp.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&amp;scope=bot</code>.</p>
View
@@ -85,6 +85,7 @@ class Bot extends Eris.Client {
if (options.gracefulExit === true) {
process.on('SIGINT', () => {
+ this.logger.info('SIGINT detected, shutting down');
let destroyAll = this.commandPlugins.map(p => p.destroy()).concat( this.eventPlugins.map(p => p.destroy()), this.middleware.map(m => m.destroy()))
this.disconnect({reconnect: false});
Promise.all(destroyAll).then(() => process.exit(0)).catch(error => {
View
@@ -1,18 +1,54 @@
const chalk = new (require('chalk')).constructor({enabled: true});
-/** Displays text to the console */
+/**
+ * Displays text to the console. Also supports sentry
+ * @prop {RavenClient} raven The Raven client used for sentry, if enabled.
+ */
class Logger {
/**
* Creates a new Logger
* @param {Object} [options] The configuration for the logger
- * @param {Boolean} [options.showDebug=false] Display `debug()` in the console
- * @param {Boolean} [options.onlyErrors=false] Only display `error()` in the console
* @param {Boolean} [options.timestamps=false] Add a timestamp to each message logged
+ * @param {Object} [options.levels] An object deciding what log levels should display
+ * @param {Boolean} [options.levels.log=true] Show `log()`
+ * @param {Boolean} [options.levels.info=true] Show `info()`
+ * @param {Boolean} [options.levels.debug=false] Show `debug()`
+ * @param {Boolean} [options.levels.warning=true] Show `warn()`
+ * @param {Boolean} [options.levels.error=true] Show `error()`
+ * @param {Object} [options.raven] Raven options. If falsy, raven will not be required.
+ * @param {String} options.raven.url Your project's sentry.io url
+ * @param {Object} [options.raven.config] A custom raven config. For more info check {@link https://docs.sentry.io/clients/node/config/|their docs}
+ * @param {Boolean} [options.raven.info=false] Send `info()` messages to sentry
+ * @param {Boolean} [options.raven.debug=false] Send `debug()` messages to sentry
+ * @param {Boolean} [options.raven.warning=false] Send `warn()` messages to sentry. If an {@link Error} is passed in the arguments, that will be sent alone.
+ * @param {Boolean} [options.raven.error=true] Send `error()` errors to sentry. If an {@link Error} is passed in the arguments, that will be sent alone.
*/
constructor(options = {}) {
- this.showDebug = !!options.showDebug;
- this.onlyErrors = !!options.onlyErrors;
- this.timestamps = !!options.timestamps
+ this.timestamps = !!options.timestamps;
+ this.levels = {
+ 'log': options.log !== undefined ? options.log : true,
+ 'info': options.info !== undefined ? options.info : true,
+ 'debug': options.debug !== undefined ? options.debug : false,
+ 'warning': options.warning !== undefined ? options.warning : true,
+ 'error': options.error !== undefined ? options.error : true
+ }
+
+ if (options.raven) {
+ this.raven = require('raven');
+ this.raven.disableConsoleAlerts();
+ this.raven.config(options.raven.url, options.raven.config || {
+ release: (require('./package.json')).version,
+ autoBreadcrumbs: { 'http': true },
+ captureUnhandledRejections: true
+ }).install();
+
+ this.ravenLevels = {
+ 'info': options.raven.info !== undefined ? options.raven.info : false,
+ 'debug': options.raven.debug !== undefined ? options.raven.debug : false,
+ 'warning': options.raven.warning !== undefined ? options.raven.warning : false,
+ 'error': options.raven.error !== undefined ? options.raven.error : true
+ }
+ }
}
/**
@@ -25,31 +61,48 @@ class Logger {
/** Log generic text */
log() {
- if (!this.onlyErrors)
+ if (this.levels.log)
console.log(this.timestamp + Array.prototype.join.call(arguments, ' '));
}
/** Log informal text */
info() {
- if (!this.onlyErrors)
+ if (this.levels.info)
console.info(this.timestamp + chalk.cyan(...arguments));
+
+ if (this.ravenLevels && this.ravenLevels.info)
+ this.raven.captureMessage(Array.prototype.join.call(arguments, ' '), { level: 'info' });
}
/** Log debugging information */
debug() {
- if (this.showDebug)
+ if (this.levels.debug)
console.log(this.timestamp + chalk.gray(...arguments));
+
+ if (this.ravenLevels && this.ravenLevels.debug)
+ this.raven.captureMessage(Array.prototype.join.call(arguments, ' '), { level: 'debug' });
}
/** Log a warning */
warn() {
- if (!this.onlyErrors)
+ if (this.levels.warning)
console.warn(this.timestamp + chalk.yellow(...arguments));
+
+ if (this.ravenLevels && this.ravenLevels.warning) {
+ let error = Array.prototype.find.call(arguments, a => a instanceof Error) || Array.prototype.join.call(arguments, ' ');
+ this.raven.captureMessage(error, { level: 'warning' });
+ }
}
/** Log an error */
error() {
- console.error(this.timestamp + chalk.red([...arguments].map(e => e instanceof Error ? e.stack : e).join(' ')));
+ if (this.levels.error)
+ console.error(this.timestamp + chalk.red([...arguments].map(e => e instanceof Error ? e.stack : e).join(' ')));
+
+ if (this.ravenLevels && this.ravenLevels.error) {
+ let error = Array.prototype.find.call(arguments, a => a instanceof Error) || Array.prototype.join.call(arguments, ' ');
+ this.raven.captureException(error);
+ }
}
}
View
@@ -1,6 +1,6 @@
{
"name": "mirai-bot-core",
- "version": "4.1.0",
+ "version": "4.2.0",
"description": "A Discord bot core allowing easy plugging in of modules",
"author": "brussell98",
"main": "index.js",
@@ -39,7 +39,8 @@
},
"optionalDependencies": {
"eventemitter3": "^2.0.2",
- "bluebird": "^3.4.6"
+ "bluebird": "^3.4.6",
+ "raven": "^1.1.1"
},
"devDependencies": {
"eslint": "*",

0 comments on commit 7d75f81

Please sign in to comment.