Skip to content
This repository has been archived by the owner on Dec 27, 2018. It is now read-only.

Commit

Permalink
Exports logger and extend configuration, closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
emmenko committed Feb 20, 2014
1 parent b6e548d commit ecf0528
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ logConfig: {
response: resSerializer // function that maps the response object with fields (status, headers, body)
src: false // includes a log of the call source location (file, line, function).
// Determining the source call is slow, therefor it's recommended not to enable this on production.
streams: [ // a list of streams that defines the type of output for log messages
{level: 'info', stream: process.stdout}
{level: 'debug', path: './sphere-node-connect-debug.log'}
]
}
```

Expand Down
58 changes: 36 additions & 22 deletions src/coffee/logger.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ bunyan = require 'bunyan'

module.exports = class

@appName: 'sphere-node-connect'

@levelStream: 'info'

@levelFile: 'debug'

@path: './sphere-node-connect-debug.log'

###*
* https://www.npmjs.org/package/bunyan
*
Expand All @@ -17,44 +25,50 @@ module.exports = class
* (https://github.com/trentm/node-bunyan#serializers)
* - src: includes a log of the call source location (file, line, function). Determining the source call
* is slow, therefor it's recommended not to enable this on production.
* - streams: a list of streams that defines the type of output for log messages
* (default:
* 'stream': 'info' -> stdout
* 'file': 'debug' -> file (path)
* )
*
* @param {Object} [config] The configuration for the logger
* @return {Object} A {Bunyan} logger
###
@init: (config = {}) ->
@init: (config = {}) =>

{levelStream, levelFile, path, logger, name, serializers, src} = _.defaults config,
levelStream: 'info'
levelFile: 'debug'
path: './sphere-node-connect-debug.log'
name: 'sphere-node-connect'
{levelStream, levelFile, path, logger, name, serializers, src, streams} = _.defaults config,
levelStream: @levelStream
levelFile: @levelFile
path: @path
name: @appName
serializers: _.extend bunyan.stdSerializers,
request: reqSerializer
response: resSerializer
request: @reqSerializer
response: @resSerializer
src: false # never use this option on production
streams: []

if logger
logger = logger.child widget_type: 'sphere-node-connect'
logger = logger.child widget_type: @appName
else
logger = bunyan.createLogger
name: name
serializers: serializers
src: src
streams: [
serializers: serializers
streams: _.extend streams, [
{level: levelStream, stream: process.stdout}
{level: levelFile, path: path}
]

logger

reqSerializer = (req) ->
type: 'REQUEST'
uri: req.uri
method: req.method
headers: req.headers

resSerializer = (res) ->
type: 'RESPONSE'
status: res.statusCode
headers: res.headers
body: res.body
@reqSerializer: (req) ->
type: 'REQUEST'
uri: req.uri
method: req.method
headers: req.headers

@resSerializer: (res) ->
type: 'RESPONSE'
status: res.statusCode
headers: res.headers
body: res.body
3 changes: 2 additions & 1 deletion src/coffee/main.coffee
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
exports.OAuth2 = require './oauth2'
exports.Rest = require './rest'
exports.Rest = require './rest'
exports.Logger = require './logger'
6 changes: 4 additions & 2 deletions src/spec/exports.spec.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{ Rest } = require '../lib/main'
{ OAuth2 } = require '../lib/main'
{Rest, OAuth2, Logger} = require '../lib/main'

describe 'exports', ->

Expand All @@ -8,3 +7,6 @@ describe 'exports', ->

it 'OAuth2', ->
expect(OAuth2).toBeDefined()

it 'Logger', ->
expect(Logger).toBeDefined()

0 comments on commit ecf0528

Please sign in to comment.