Skip to content
Closed
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ for information codes.
GET /dev 200 0.224 ms - 2
```

Set the `NO_COLOR` environment variable to a non-empty value to disable the
colored output (see [no-color.org](https://no-color.org/)). The same fields are
then printed without any ANSI escape sequences.

##### short

Shorter than default, also including response time.
Expand Down
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ morgan.format('tiny', ':method :url :status :res[content-length] - :response-tim
*/

morgan.format('dev', function developmentFormatLine (tokens, req, res) {
// NO_COLOR (https://no-color.org/): when present and not an empty
// string, regardless of its value, ANSI color codes must not be added
if (process.env.NO_COLOR) {
var plainFn = developmentFormatLine.plain || (developmentFormatLine.plain =

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The nullish coalescing operator would be preferable for the expression too, although I don't think it is supported by versions of Node as old as what Morgan targets.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

correct, cannot use ?? in this release line

compile(':method :url :status :response-time ms - :res[content-length]'))
Comment on lines +217 to +218

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Although it does match the behaviour elsewhere in this function, adding attributes to the function seems like an extremely strange design decision. It's not type-safe, and makes it quite unclear where the definitions actually come from.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yeah it's house style and not worth a rewrite for this, but these are internal if that makes you feel any better about the type safety of it


return plainFn(tokens, req, res)
}

// get the status code if response written
var status = headersSent(res)
? res.statusCode
Expand Down
32 changes: 32 additions & 0 deletions test/morgan.js
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,38 @@ describe('morgan()', function () {
.expect(200, cb)
})
})

describe('with NO_COLOR environment variable set', function () {
beforeEach(function () {
process.env.NO_COLOR = '1'
})

afterEach(function () {
delete process.env.NO_COLOR
})

it('should not emit any ANSI escape codes', function (done) {
var cb = after(2, function (err, res, line) {
if (err) return done(err)
assert.strictEqual(line.indexOf('\x1b'), -1)
assert.ok(/^GET \/ 200 \d+\.\d{3} ms - -$/.test(line), 'unexpected line: ' + line)
done()
})

var stream = createLineStream(function onLine (line) {
cb(null, null, line)
})

var server = createServer('dev', { stream: stream }, function (req, res, next) {
res.statusCode = 200
next()
})

request(server)
.get('/')
.expect(200, cb)
})
})
})

describe('short', function () {
Expand Down