Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logger #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ server.start({
});
```

You can configure logging options as well via `logger`

```js
server.start({
logger: {
level: 'ALL',
appenders: [
{ type: 'console' }
]
},
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just a couple more changes, then it's ready to merge! 🎉 . Should probably add an example here of using a logger default values.

```

We use the (https://github.com/nomiddlename/log4js-node/wiki/Connect-Logger)[log4js] connect logger. Learn more about the available options from their project. Be sure to set `options.logger.level` to enable the logger.


## Global Install

Expand Down
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ var path = require('path')
var serveStatic = require('serve-static')
var serveStaticFile = require('connect-static-file')
var compression = require('compression')
var log4js = require('log4js')
var app = connect()

const PORT = 9000
const DIRECTORY = 'public'
const FILE = 'index.html'
const HOST = '0.0.0.0'
const LOGGEROPTIONS = {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the default value for this should be false. Then we could check if it's true or an object, then attach the logger.


exports.start = function (options, _onStarted) {
options = options || {}
Expand All @@ -20,16 +22,25 @@ exports.start = function (options, _onStarted) {
let directories = options.directories || [directory]
let file = options.file || FILE
let host = options.host || HOST
let loggerOptions = options.logger || LOGGEROPTIONS
let onStarted = _onStarted || function () {}

app.use(compression())

// First, check the file system
// configure logger
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably only use the logger if they provide a config for it (not default to all the time)

log4js.configure(loggerOptions)
if (loggerOptions.level) {
const logger = log4js.getLogger()
logger.setLevel(loggerOptions.level)
app.use(log4js.connectLogger(logger, { level: log4js.levels[loggerOptions.level] }))
}

// check the file system
directories.forEach(function(directory) {
app.use(serveStatic(directory, { extensions: ['html'] }))
})

// Then, serve the fallback file
// serve the fallback file
app.use(serveStaticFile(path.join(directory, file)))

return app.listen(port, host, function (err) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"compression": "1.6.2",
"connect": "3.5.0",
"connect-static-file": "1.1.2",
"log4js": "1.1.0",
"serve-static": "1.11.2"
},
"devDependencies": {
Expand Down