Skip to content

Commit

Permalink
docs: add example for log rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Apr 8, 2015
1 parent bcd2a18 commit bba89d7
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ http.createServer(function (req, res) {

### write logs to a file

Simple app that will log all request in the Apache combined format to the file "access.log"
#### single file

Simple app that will log all requests in the Apache combined format to the file
`access.log`.

```js
var express = require('express')
Expand All @@ -224,6 +227,39 @@ app.get('/', function (req, res) {
})
```

#### log file rotation

Simple app that will log all requests in the Apache combined format to one log
file per date in the `log/` directory using the
[file-stream-rotator module](https://www.npmjs.com/package/file-stream-rotator).

```js
var createRotatingStream = require('file-stream-rotator').getStream
var express = require('express')
var fs = require('fs')
var morgan = require('morgan')

var app = express()
var logDirectory = __dirname + '/log'

// ensure log directory exists
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)

// create a rotating write stream
var accessLogStream = createRotatingStream({
filename: logDirectory + '/access-%DATE%.log',
frequency: 'daily',
verbose: false
})

// setup the logger
app.use(morgan('combined', {stream: accessLogStream}))

app.get('/', function (req, res) {
res.send('hello, world!')
})
```

### use custom token formats

Sample app that will use custom token formats. This adds an ID to all requests and displays it using the `:id` token.
Expand Down

0 comments on commit bba89d7

Please sign in to comment.