Skip to content

Commit cc6c094

Browse files
lukekarrysfritzy
authored andcommitted
feat: add logs-dir config to set custom logging location
This also allows logs-max to be set to 0 to disable log file writing. Closes #4466 Closes #4206
1 parent 81afa5a commit cc6c094

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2748
-815
lines changed

docs/content/using-npm/config.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,13 +1027,26 @@ See also the `foreground-scripts` config.
10271027
<!-- automatically generated, do not edit manually -->
10281028
<!-- see lib/utils/config/definitions.js -->
10291029

1030+
#### `logs-dir`
1031+
1032+
* Default: A directory named `_logs` inside the cache
1033+
* Type: null or Path
1034+
1035+
The location of npm's log directory. See [`npm logging`](/using-npm/logging)
1036+
for more information.
1037+
1038+
<!-- automatically generated, do not edit manually -->
1039+
<!-- see lib/utils/config/definitions.js -->
1040+
10301041
#### `logs-max`
10311042

10321043
* Default: 10
10331044
* Type: Number
10341045

10351046
The maximum number of log files to store.
10361047

1048+
If set to 0, no log files will be written for the current run.
1049+
10371050
<!-- automatically generated, do not edit manually -->
10381051
<!-- see lib/utils/config/definitions.js -->
10391052

@@ -1628,9 +1641,9 @@ particular, use care when overriding this setting for public packages.
16281641
* Default: false
16291642
* Type: Boolean
16301643

1631-
If true, writes an `npm-debug` log to `_logs` and timing information to
1632-
`_timing.json`, both in your cache, even if the command completes
1633-
successfully. `_timing.json` is a newline delimited list of JSON objects.
1644+
If true, writes a debug log to `logs-dir` and timing information to
1645+
`_timing.json` in the cache, even if the command completes successfully.
1646+
`_timing.json` is a newline delimited list of JSON objects.
16341647

16351648
You can quickly view it with this [json](https://npm.im/json) command line:
16361649
`npm exec -- json -g < ~/.npm/_timing.json`.

docs/content/using-npm/logging.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
---
22
title: Logging
33
section: 7
4-
description: Why, What & How we Log
4+
description: Why, What & How We Log
55
---
66

77
### Description
88

99
The `npm` CLI has various mechanisms for showing different levels of information back to end-users for certain commands, configurations & environments.
1010

11+
### Setting Log File Location
12+
13+
All logs are written to a debug log, with the path to that file printed if the execution of a command fails.
14+
15+
The default location of the logs directory is a directory named `_logs` inside the npm cache. This can be changed
16+
with the `logs-dir` config option.
17+
18+
Log files will be removed from the `logs-dir` when the number of log files exceeds `logs-max`, with the oldest logs being deleted first.
19+
20+
To turn off logs completely set `--logs-max=0`.
21+
1122
### Setting Log Levels
1223

1324
#### `loglevel`
@@ -28,8 +39,6 @@ The default value of `loglevel` is `"notice"` but there are several levels/types
2839

2940
All logs pertaining to a level proceeding the current setting will be shown.
3041

31-
All logs are written to a debug log, with the path to that file printed if the execution of a command fails.
32-
3342
##### Aliases
3443

3544
The log levels listed above have various corresponding aliases, including:
@@ -47,6 +56,15 @@ The log levels listed above have various corresponding aliases, including:
4756

4857
The `npm` CLI began hiding the output of lifecycle scripts for `npm install` as of `v7`. Notably, this means you will not see logs/output from packages that may be using "install scripts" to display information back to you or from your own project's scripts defined in `package.json`. If you'd like to change this behavior & log this output you can set `foreground-scripts` to `true`.
4958

59+
### Timing Information
60+
61+
The `--timing` config can be set which does two things:
62+
63+
1. Always shows the full path to the debug log regardless of command exit status
64+
1. Write timing information to a timing file in the cache or `logs-dir`
65+
66+
This file is a newline delimited list of JSON objects that can be inspected to see timing data for each task in a `npm` CLI run.
67+
5068
### Registry Response Headers
5169

5270
#### `npm-notice`
@@ -55,6 +73,15 @@ The `npm` CLI reads from & logs any `npm-notice` headers that are returned from
5573

5674
This header is not cached, and will not be logged if the request is served from the cache.
5775

76+
### Logs and Sensitive Information
77+
78+
The `npm` CLI makes a best effort to redact the following from terminal output and log files:
79+
80+
- Passwords inside basic auth URLs
81+
- npm tokens
82+
83+
However, this behavior should not be relied on to keep all possible sensitive information redacted. If you are concerned about secrets in your log file or terminal output, you can use `--loglevel=silent` and `--logs-max=0` to ensure no logs are written to your terminal or filesystem.
84+
5885
### See also
5986

6087
* [config](/using-npm/config)

lib/cli.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ module.exports = async process => {
3030
}
3131

3232
const log = require('./utils/log-shim.js')
33-
const replaceInfo = require('./utils/replace-info.js')
34-
log.verbose('cli', replaceInfo(process.argv))
35-
33+
// only log node and npm paths in argv initially since argv can contain
34+
// sensitive info. a cleaned version will be logged later
35+
log.verbose('cli', process.argv.slice(0, 2).join(' '))
3636
log.info('using', 'npm@%s', npm.version)
3737
log.info('using', 'node@%s', process.version)
3838

39-
const updateNotifier = require('./utils/update-notifier.js')
40-
4139
let cmd
4240
// now actually fire up npm and run the command.
4341
// this is how to use npm programmatically:
@@ -54,8 +52,6 @@ module.exports = async process => {
5452
npm.config.set('usage', false, 'cli')
5553
}
5654

57-
updateNotifier(npm)
58-
5955
cmd = npm.argv.shift()
6056
if (!cmd) {
6157
npm.output(await npm.usage)

lib/commands/bin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const log = require('../utils/log-shim.js')
12
const envPath = require('../utils/path.js')
23
const BaseCommand = require('../base-command.js')
34

@@ -11,8 +12,7 @@ class Bin extends BaseCommand {
1112
const b = this.npm.bin
1213
this.npm.output(b)
1314
if (this.npm.config.get('global') && !envPath.includes(b)) {
14-
// XXX: does this need to be console?
15-
console.error('(not in PATH env variable)')
15+
log.error('bin', '(not in PATH env variable)')
1616
}
1717
}
1818
}

lib/commands/doctor.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,6 @@ class Doctor extends BaseCommand {
131131

132132
if (!this.npm.silent) {
133133
this.npm.output(table(outTable, tableOpts))
134-
if (!allOk) {
135-
// TODO is this really needed?
136-
console.error('')
137-
}
138134
}
139135
if (!allOk) {
140136
throw new Error('Some problems found. See above for recommendations.')

lib/commands/view.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* eslint-disable no-console */
2+
// XXX: remove console.log later
3+
14
// npm view [pkg [pkg ...]]
25

36
const color = require('ansicolors')

0 commit comments

Comments
 (0)