-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logging): improve logging, especially for errors (#56)
- Loading branch information
Kent C. Dodds
authored
May 18, 2017
1 parent
9029593
commit d0345e1
Showing
8 changed files
with
224 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<!-- | ||
Thanks for your interest in the project. I appreciate bugs filed and PRs submitted! | ||
Please make sure that you are familiar with and follow the Code of Conduct for | ||
this project (found in the CODE_OF_CONDUCT.md file). | ||
Please fill out this template with all the relevant information so we can | ||
understand what's going on and fix the issue. | ||
I'll probably ask you to submit the fix (after giving some direction). If you've | ||
never done that before, that's great! Check this free short video tutorial to | ||
learn how: http://kcd.im/pull-request | ||
--> | ||
|
||
- `prettier-eslint-cli` version: | ||
- `prettier` version: | ||
- `eslint` version: | ||
|
||
Relevant code/config. | ||
|
||
```javascript | ||
|
||
``` | ||
|
||
What you did: | ||
|
||
|
||
|
||
What happened: | ||
|
||
<!-- Please provide the full error message/screenshots/anything --> | ||
|
||
Reproduction: | ||
|
||
<!-- | ||
Please reproduce your issue by forking this repository: | ||
https://github.com/kentcdodds/prettier-eslint-cli-repro | ||
with as minimal amount of code possible. Then paste a link below: | ||
--> | ||
|
||
-- paste your link here -- | ||
|
||
Problem description: | ||
|
||
|
||
|
||
Suggested solution: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!-- | ||
Thanks for your interest in the project. I appreciate bugs filed and PRs submitted! | ||
Please make sure that you are familiar with and follow the Code of Conduct for | ||
this project (found in the CODE_OF_CONDUCT.md file). | ||
Also, please make sure you're familiar with and follow the instructions in the | ||
contributing guidelines (found in the CONTRIBUTING.md file). | ||
If you're new to contributing to open source projects, you might find this free | ||
video course helpful: http://kcd.im/pull-request | ||
Please fill out the information below to expedite the review and (hopefully) | ||
merge of your pull request! | ||
--> | ||
|
||
<!-- What changes are being made? (What feature/bug is being fixed here?) --> | ||
**What**: | ||
|
||
<!-- Why are these changes necessary? --> | ||
**Why**: | ||
|
||
<!-- How were these changes implemented? --> | ||
**How**: | ||
|
||
|
||
<!-- feel free to add additional comments --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`logs a check for trace 1`] = ` | ||
"There has been an unknown error when running the prettier-eslint CLI. If it's unclear to you what went wrong, then try this: | ||
✅ Run the script again with the LOG_LEVEL environment variable set to \\"trace\\" | ||
2. Search existing issues on GitHub: https://github.com/prettier/prettier-eslint-cli/issues?utf8=%E2%9C%93&q=my%20error | ||
3. Make a minimal reproduction in a totally separate repository. You can fork this one: https://github.com/kentcdodds/prettier-eslint-cli-repro | ||
4. Post an issue with a link to your reproduction to the issues on GitHub: https://github.com/prettier/prettier-eslint-cli/issues/new" | ||
`; | ||
|
||
exports[`logs all options 1`] = ` | ||
"There has been an unknown error when running the prettier-eslint CLI. If it's unclear to you what went wrong, then try this: | ||
1. Run the script again with the LOG_LEVEL environment variable set to \\"trace\\" | ||
2. Search existing issues on GitHub: https://github.com/prettier/prettier-eslint-cli/issues?utf8=%E2%9C%93&q=my%20error | ||
3. Make a minimal reproduction in a totally separate repository. You can fork this one: https://github.com/kentcdodds/prettier-eslint-cli-repro | ||
4. Post an issue with a link to your reproduction to the issues on GitHub: https://github.com/prettier/prettier-eslint-cli/issues/new" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* istanbul ignore-next */ | ||
import onUncaughtException from './uncaught-exception-handler' | ||
|
||
process.on('uncaughtException', onUncaughtException) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
#!/usr/bin/env node | ||
|
||
// eslint-disable-next-line import/no-unassigned-import | ||
import './add-exception-handler' // want to do this first | ||
import getLogger from 'loglevel-colored-level-prefix' | ||
import parser from './parser' | ||
import formatFiles from './format-files' | ||
|
||
const argv = parser.parse(process.argv.slice(2)) | ||
const logger = getLogger({prefix: 'prettier-eslint-cli'}) | ||
const args = process.argv.slice(2) | ||
|
||
logger.trace('Parsing args: ', args) | ||
const argv = parser.parse(args) | ||
|
||
formatFiles(argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {oneLine, oneLineTrim} from 'common-tags' | ||
import getLogger from 'loglevel-colored-level-prefix' | ||
|
||
const logger = getLogger({prefix: 'prettier-eslint-cli'}) | ||
|
||
export default onUncaughtException | ||
|
||
function onUncaughtException(err) { | ||
const level = logger.getLevel() | ||
const isTrace = level === 0 | ||
const traceResolution = oneLine` | ||
Run the script again with the LOG_LEVEL | ||
environment variable set to "trace" | ||
` | ||
const resolutionSteps = [ | ||
`${isTrace ? '✅ ' : '1.'} ${traceResolution}`, | ||
oneLine` | ||
2. Search existing issues on GitHub: | ||
${oneLineTrim` | ||
https://github.com/prettier/prettier-eslint-cli/issues | ||
?utf8=%E2%9C%93&q=${encodeURIComponent(err.message)} | ||
`} | ||
`, | ||
oneLine` | ||
3. Make a minimal reproduction in a totally separate repository. | ||
You can fork this one: | ||
https://github.com/kentcdodds/prettier-eslint-cli-repro | ||
`, | ||
oneLine` | ||
4. Post an issue with a link to your reproduction to the issues | ||
on GitHub: https://github.com/prettier/prettier-eslint-cli/issues/new | ||
`, | ||
].join('\n ') | ||
logger.error( | ||
oneLine` | ||
There has been an unknown error when running the prettier-eslint CLI. | ||
If it's unclear to you what went wrong, then try this: | ||
`, | ||
`\n ${resolutionSteps}`, | ||
) | ||
throw err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import getLoggerMock from 'loglevel-colored-level-prefix' | ||
import onUncaughtException from './uncaught-exception-handler' | ||
|
||
jest.mock('loglevel-colored-level-prefix', () => { | ||
const logger = {} | ||
const __mock__ = {logger, level: 4, resetAll} | ||
const getLogger = jest.fn(() => resetAll()) | ||
getLogger.__mock__ = __mock__ | ||
return getLogger | ||
|
||
function resetAll() { | ||
getLogger.mockClear() | ||
Object.assign(logger, { | ||
getLevel: jest.fn(() => getLogger.__mock__.level), | ||
trace: jest.fn(), | ||
debug: jest.fn(), | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
}) | ||
return logger | ||
} | ||
}) | ||
|
||
beforeEach(() => { | ||
getLoggerMock.__mock__.resetAll() | ||
}) | ||
|
||
test('logs all options', () => { | ||
const logger = getLoggerMock() | ||
runWithCatch(new Error('my error')) | ||
expect(logger.error).toHaveBeenCalledTimes(1) | ||
const errorLog = logger.error.mock.calls[0].join(' ') | ||
expect(errorLog).toMatchSnapshot() | ||
}) | ||
|
||
test('logs a check for trace', () => { | ||
getLoggerMock.__mock__.level = 0 | ||
const logger = getLoggerMock() | ||
runWithCatch(new Error('my error')) | ||
expect(logger.error).toHaveBeenCalledTimes(1) | ||
const errorLog = logger.error.mock.calls[0].join(' ') | ||
expect(errorLog).toContain('✅') | ||
expect(errorLog).toMatchSnapshot() | ||
}) | ||
|
||
test('re-throws the given error', () => { | ||
const myError = new Error('my error') | ||
expect(() => onUncaughtException(myError)).toThrow(myError) | ||
}) | ||
|
||
function runWithCatch(...args) { | ||
try { | ||
onUncaughtException(...args) | ||
} catch (e) { | ||
// ignore | ||
} | ||
} |