Browse Source

log.format, error reporting

master
Lloyd Brookes 9 years ago
parent
commit
3e54a492f7
  1. 2
      README.md
  2. 8
      doc/logging.md
  3. 2
      doc/visualisation.md
  4. 27
      lib/local-web-server.js
  5. 2
      package.json
  6. 8
      test/compress/big-file.txt

2
README.md

@ -71,7 +71,7 @@ local-web-server is a command-line tool. To serve the current directory, run `ws
-b, --forbid path ... A list of forbidden routes. -b, --forbid path ... A list of forbidden routes.
-n, --no-cache Disable etag-based caching -forces loading from disk each request. -n, --no-cache Disable etag-based caching -forces loading from disk each request.
-c, --compress Serve gzip-compressed resources, where applicable. -c, --compress Serve gzip-compressed resources, where applicable.
-f, --log-format string If a format is supplied an access log is written to stdout. If not, a dynamic
-f, --log.format string If a format is supplied an access log is written to stdout. If not, a dynamic
statistics view is displayed. Use a preset ('none', 'dev','combined', statistics view is displayed. Use a preset ('none', 'dev','combined',
'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method -> 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method ->
:url'). :url').

8
doc/logging.md

@ -1,13 +1,13 @@
# Logging # Logging
By default, local-web-server outputs a simple, dynamic statistics view. To see traditional web server logs, use `--log-format`:
By default, local-web-server outputs a simple, dynamic statistics view. To see traditional web server logs, use `--log.format`:
```sh ```sh
$ ws --log-format combined
$ ws --log.format combined
serving at http://localhost:8000 serving at http://localhost:8000
::1 - - [16/Nov/2015:11:16:52 +0000] "GET / HTTP/1.1" 200 12290 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2562.0 Safari/537.36" ::1 - - [16/Nov/2015:11:16:52 +0000] "GET / HTTP/1.1" 200 12290 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2562.0 Safari/537.36"
``` ```
The format value supplied is passed directly to [morgan](https://github.com/expressjs/morgan). The exception is `--log-format none` which disables all output.
The format value supplied is passed directly to [morgan](https://github.com/expressjs/morgan). The exception is `--log.format none` which disables all output.
# Visualisation # Visualisation
@ -17,7 +17,7 @@ To get live statistics in [goaccess](http://goaccess.io/), first create this con
``` ```
time-format %T time-format %T
date-format %d/%b/%Y date-format %d/%b/%Y
log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
log.format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
``` ```
Then, start the server, outputting `combined` format logs to disk: Then, start the server, outputting `combined` format logs to disk:

2
doc/visualisation.md

@ -4,7 +4,7 @@ To get live statistics in [goaccess](http://goaccess.io/), first create this con
``` ```
time-format %T time-format %T
date-format %d/%b/%Y date-format %d/%b/%Y
log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
log.format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
``` ```
Then, start the server, outputting `combined` format logs to disk: Then, start the server, outputting `combined` format logs to disk:

27
lib/local-web-server.js

@ -77,11 +77,16 @@ class LocalWebServer {
const usage = commandLineUsage(cli.usage(middlewareOptionDefinitions)) const usage = commandLineUsage(cli.usage(middlewareOptionDefinitions))
let options = {} let options = {}
const allOptionDefinitions = cli.optionDefinitions.concat(middlewareOptionDefinitions)
try { try {
options = commandLineArgs(cli.optionDefinitions.concat(middlewareOptionDefinitions))
options = commandLineArgs(allOptionDefinitions)
} catch (err) { } catch (err) {
tool.printError(err)
tool.printError(allOptionDefinitions.map(def => {
return `name: ${def.name}${def.alias ? ', alias: ' + def.alias : ''}`
}).join('\n'))
console.error(usage) console.error(usage)
tool.halt(err)
tool.halt()
} }
/* combine in stored config */ /* combine in stored config */
@ -125,7 +130,7 @@ class LocalWebServer {
const app = new Koa() const app = new Koa()
app.use(this.stack) app.use(this.stack)
app.on('error', err => { app.on('error', err => {
if (this.options['log-format']) {
if (this.options['log.format']) {
console.error(ansi.format(err.stack, 'red')) console.error(ansi.format(err.stack, 'red'))
} }
}) })
@ -189,7 +194,6 @@ function onServerUp (port, directory, isHttps) {
)) ))
} }
function getIPList () { function getIPList () {
const flatten = require('reduce-flatten') const flatten = require('reduce-flatten')
const os = require('os') const os = require('os')
@ -227,25 +231,36 @@ function collectUserOptions (mwOptionDefinitions) {
*/ */
function loadStack (modulePath) { function loadStack (modulePath) {
let module let module
const tried = []
if (modulePath) { if (modulePath) {
const fs = require('fs') const fs = require('fs')
try { try {
tried.push(path.resolve(modulePath))
module = require(path.resolve(modulePath)) module = require(path.resolve(modulePath))
} catch (err) { } catch (err) {
const walkBack = require('walk-back') const walkBack = require('walk-back')
const foundPath = walkBack(process.cwd(), path.join('node_modules', 'local-web-server-' + modulePath)) const foundPath = walkBack(process.cwd(), path.join('node_modules', 'local-web-server-' + modulePath))
tried.push('local-web-server-' + modulePath)
if (foundPath) { if (foundPath) {
module = require(foundPath) module = require(foundPath)
} else { } else {
const foundPath2 = walkBack(process.cwd(), path.join('node_modules', modulePath)) const foundPath2 = walkBack(process.cwd(), path.join('node_modules', modulePath))
tried.push(modulePath)
if (foundPath2) { if (foundPath2) {
module = require(foundPath2) module = require(foundPath2)
} }
} }
} }
} }
if (!(module && (module.prototype.middleware || module.prototype.stack))) {
tool.halt(new Error('Not valid Middleware: ' + modulePath))
if (module) {
if (!(module.prototype.middleware || module.prototype.stack)) {
const insp = require('util').inspect(module, { depth: 3, colors: true })
const msg = `Not valid Middleware at: ${insp}`
tool.halt(new Error(msg))
}
} else {
const msg = `No module found at: \n${tried.join('\n')}`
tool.halt(new Error(msg))
} }
return module return module
} }

2
package.json

@ -34,7 +34,7 @@
"dependencies": { "dependencies": {
"ansi-escape-sequences": "^2.2.2", "ansi-escape-sequences": "^2.2.2",
"array-back": "^1.0.3", "array-back": "^1.0.3",
"command-line-tool": "~0.3.1",
"command-line-tool": "75lb/command-line-tool",
"config-master": "^2.0.3", "config-master": "^2.0.3",
"koa": "^2.0.0", "koa": "^2.0.0",
"local-web-server-default-stack": "github:local-web-server/default-stack", "local-web-server-default-stack": "github:local-web-server/default-stack",

8
test/compress/big-file.txt

@ -54,7 +54,7 @@ $ ws --help
Server Server
-p, --port <number> Web server port -p, --port <number> Web server port
-f, --log-format <string> If a format is supplied an access log is written to stdout. If not, a statistics view is displayed. Use a
-f, --log.format <string> If a format is supplied an access log is written to stdout. If not, a statistics view is displayed. Use a
preset ('none', 'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method -> preset ('none', 'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method ->
:url'). :url').
-d, --directory <string> Root directory, defaults to the current directory -d, --directory <string> Root directory, defaults to the current directory
@ -90,11 +90,11 @@ $ ws --compress
``` ```
### Logging ### Logging
Passing a value to `--log-format` will write an access log to `stdout`.
Passing a value to `--log.format` will write an access log to `stdout`.
Either use a built-in [morgan](https://github.com/expressjs/morgan) logger preset: Either use a built-in [morgan](https://github.com/expressjs/morgan) logger preset:
```sh ```sh
$ ws --log-format short
$ ws --log.format short
``` ```
Or a custom [morgan](https://github.com/expressjs/morgan) log format: Or a custom [morgan](https://github.com/expressjs/morgan) log format:
@ -123,7 +123,7 @@ Or in a `.local-web-server.json` file stored in the directory you want to serve
```json ```json
{ {
"port": 8100, "port": 8100,
"log-format": "tiny"
"log.format": "tiny"
} }
``` ```

Loading…
Cancel
Save