Skip to content

Commit

Permalink
logger singleton style instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
nucleardreamer committed Jan 27, 2017
1 parent a97002e commit 5747996
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 107 deletions.
12 changes: 10 additions & 2 deletions .README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ Head on over to the [documentation](https://oaklabsinc.github.io/oak-tools/) pag
The logger is based off [pino](https://github.com/pinojs/pino) and has nearly the same same option parameters. The following example are the default options.

```javascript
const Logger = require('oak-tools').logger
let log = new Logger({
const { logger } = require('oak-tools')
let log = logger({
level: 'info',
stream: process.stdout,
pretty: true
})
log.info('sup')
log.error({
err: new Error('crap...')
})
log.debug({
msg: 'debug message here',
now: Date.now()
})

```

Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ Head on over to the [documentation](https://oaklabsinc.github.io/oak-tools/) pag
The logger is based off [pino](https://github.com/pinojs/pino) and has nearly the same same option parameters. The following example are the default options.

```javascript
const Logger = require('oak-tools').logger
let log = new Logger({
const { logger } = require('oak-tools')
let log = logger({
level: 'info',
stream: process.stdout,
pretty: true
})
log.info('sup')
log.error({
err: new Error('crap...')
})
log.debug({
msg: 'debug message here',
now: Date.now()
})

```

Expand Down
2 changes: 1 addition & 1 deletion docs/Logger.html
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 14:15:50 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 15:39:49 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/Message.html
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 14:15:50 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 15:39:49 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/WebSocketClient.html
Original file line number Diff line number Diff line change
Expand Up @@ -2195,7 +2195,7 @@ <h4 class="name" id="event:ready">ready</h4>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 14:15:50 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 15:39:49 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/WebSocketServer.html
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ <h5>Type:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 14:15:50 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 15:39:49 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ <h5>Type:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 14:15:50 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 15:39:49 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Logger.ht
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 14:15:50 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 15:39:49 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
92 changes: 46 additions & 46 deletions docs/index.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,51 +37,51 @@ <h1 class="page-title">index.js</h1>

<section>
<article>
<pre class="prettyprint source linenums"><code>
const { join } = require('path')

/**
* oak-tools module
* @version 0.2.0
* @module oak-tools
*/

/**
* Logger object
* @param {Object} options options object
* @param {Function} callback callback function
* @returns {Logger}
*/
module.exports.logger = function () {
return require(join(__dirname, 'lib', 'logger'))
}

/**
* Message class
* @returns {Message}
*/
module.exports.message = function () {
return require(join(__dirname, 'lib', 'message'))
}

/**
* Starts a messaging server
* @param {String} type Type of server
* @returns {WebSocketServer}
*/
module.exports.server = function (type) {
return require(join(__dirname, 'lib', type, 'server'))
}

/**
* Starts a messaging client
* @param {String} type Type of client
* @returns {WebSocketClient}
*/

module.exports.client = function (type) {
return require(join(__dirname, 'lib', type, 'client'))
}
<pre class="prettyprint source linenums"><code>
const { join } = require('path')

/**
* oak-tools module
* @version 0.2.0
* @module oak-tools
*/

/**
* Logger object
* @param {Object} options options object
* @param {Function} callback callback function
* @returns {Logger}
*/
module.exports.logger = function () {
return require(join(__dirname, 'lib', 'logger')).apply(this, arguments)
}

/**
* Message class
* @returns {Message}
*/
module.exports.message = function () {
return require(join(__dirname, 'lib', 'message'))
}

/**
* Starts a messaging server
* @param {String} type Type of server
* @returns {WebSocketServer}
*/
module.exports.server = function (type) {
return require(join(__dirname, 'lib', type, 'server'))
}

/**
* Starts a messaging client
* @param {String} type Type of client
* @returns {WebSocketClient}
*/

module.exports.client = function (type) {
return require(join(__dirname, 'lib', type, 'client'))
}
</code></pre>
</article>
</section>
Expand All @@ -94,7 +94,7 @@ <h1 class="page-title">index.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 14:14:01 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 15:39:49 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
78 changes: 39 additions & 39 deletions docs/lib_logger.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,44 @@ <h1 class="page-title">lib/logger.js</h1>

<section>
<article>
<pre class="prettyprint source linenums"><code>const pino = require('pino')

/**
* logger object
* @typedef {Object} logger
* @prop {Function} trace
* @prop {Function} debug
* @prop {Function} info
* @prop {Function} error
* @prop {Function} warn
* @prop {Function} fatal
*/

/**
* Creates a logger object
* @constructor
* @param {Object} options options object
* @param {String} [options.name] set the logger instance name
* @param {String} [options.level=info] set the log level, default is info
* @param {Object} [options.stream=stdout] outbound stream, defaults to process.stdout
* @param {boolean} [options.pretty=false] pretty printing, defaults to false
* @returns {logger}
*/
function Logger ({ name = undefined, level = 'info', stream = process.stdout, pretty = false } = {}) {
if (pretty) {
let _pretty = pino.pretty()
_pretty.pipe(stream)
stream = _pretty
}

let _logger = pino(stream)
_logger.level = level
_logger.name = name

return _logger
}

module.exports = Logger
<pre class="prettyprint source linenums"><code>const pino = require('pino')

/**
* logger object
* @typedef {Object} logger
* @prop {Function} trace
* @prop {Function} debug
* @prop {Function} info
* @prop {Function} error
* @prop {Function} warn
* @prop {Function} fatal
*/

/**
* Creates a logger object
* @constructor
* @param {Object} options options object
* @param {String} [options.name] set the logger instance name
* @param {String} [options.level=info] set the log level, default is info
* @param {Object} [options.stream=stdout] outbound stream, defaults to process.stdout
* @param {boolean} [options.pretty=false] pretty printing, defaults to false
* @returns {logger}
*/
function Logger ({ name = undefined, level = 'info', stream = process.stdout, pretty = false } = {}) {
if (pretty) {
let _pretty = pino.pretty()
_pretty.pipe(stream)
stream = _pretty
}

let _logger = pino(stream)
_logger.level = level
_logger.name = name

return _logger
}

module.exports = Logger
</code></pre>
</article>
</section>
Expand All @@ -87,7 +87,7 @@ <h1 class="page-title">lib/logger.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 14:14:01 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 15:39:49 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/lib_message.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ <h1 class="page-title">lib/message.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 14:15:50 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 15:39:49 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/lib_websocket_client.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ <h1 class="page-title">lib/websocket/client.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 14:15:50 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 15:39:49 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/lib_websocket_server.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ <h1 class="page-title">lib/websocket/server.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 14:15:50 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 15:39:49 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/module-oak-tools.html
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 14:15:50 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Fri Jan 27 2017 15:39:49 GMT-0800 (PST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
Expand Down
7 changes: 4 additions & 3 deletions examples/websocket.server.example.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const { join } = require('path')
const tools = require(join(__dirname, '..'))

const Logger = tools.logger()
const log = new Logger({
const log = tools.logger({
pretty: true
})

let Server = tools.server('websocket')
let server = new Server()
let server = new Server({
port: 5050
})

server.on('error', function (err) {
log.error({ err })
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { join } = require('path')
* @returns {Logger}
*/
module.exports.logger = function () {
return require(join(__dirname, 'lib', 'logger'))
return require(join(__dirname, 'lib', 'logger')).apply(this, arguments)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oak-tools",
"version": "0.3.1",
"version": "0.3.2",
"description": "Helpful utilities for developing oak applications",
"main": "index.js",
"scripts": {
Expand Down
3 changes: 1 addition & 2 deletions test/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const { join } = require('path')
const Stream = require('stream')
const async = require('async')
const test = require('tape')
const { logger: Logger } = require(join(__dirname, '..'))
const logger = new Logger()
const { logger } = require(join(__dirname, '..'))

const levels = [
'fatal',
Expand Down
4 changes: 3 additions & 1 deletion test/websocket.server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ test('*** server', function (t) {
t.plan(10)

let WsServer = tools.server('websocket')
let server = new WsServer({}, function () {
let server = new WsServer({
port: 5000
}, function () {
t.pass('server should callback when ready')
})

Expand Down

0 comments on commit 5747996

Please sign in to comment.