question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Logging only in `fit` mode

See original GitHub issue

Do you want to request a feature or report a bug?

Feature

I often add debug logging in my programs, which I’d like to see in one specific case: fit() (focus-it) annotated tests, but I usually end up doing something like if (global.JEST_IS_FIT) console.log(x) in my source code, and global.JEST_IS_FIT = true in my test, and then remove it from the test when I delete the 'f' in 'fit'.

The code in the source code isn’t that bad, but in the test file I’d still like to enable the logging by adding a character or two. Also, it’d be good to have a consistent convention and tooling for this in all projects using jest.

I don’t have an exact proposal for how this should look in the source and test file. Just some ideas:

  • dit instead of fit, for ‘debug-it’
  • console.debug only shows up in this mode; both in the test file and source code
  • process.env.JEST_FIT or similar. For code going through webpack, DefinePlugin could be used to cause DCE

Concerns:

  • I would like this to work without restarting jest (e.g. no a flag/env var)
  • It seems cleaner to tie this to the test file instead of a hotkey in the interactive watch mode because you want to both focus the test and enable debug logging in one operation, and then remove both in one operation.
  • The code under test might call other functions that also use the debug logging mechanism

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mattphillipscommented, Mar 6, 2019

Is this something that we actually want in Jest? It seems like the problem here is trying to leave long living debugs using console.log.

This problem can be solved by using a logger that supports levels as an environment variable.

Given that we are trying to adopt: https://fishshell.com/docs/current/design.html, I’m not sure it is worth adding this to core.

I don’t think we should encourage people to add blocks of debug code to application code with Jest internals (especially if they are long living!) or to override console.debug just for tests. Both of these options will increase application code with potentially unwanted side effects, i.e. leaving a console.debug in by mistake.

Instead we should recommend using a logger with levels so that devs can confidently add logs to their code and just set the level relevant to the environment the code is being ran in.

Thoughts @SimenB @rickhanlonii?

1reaction
macknesscommented, Mar 6, 2019

Hi @SimenB, do you mind if I pick this one up?

Read more comments on GitHub >

github_iconTop Results From Across the Web

logging.handlers — Logging handlers — Python 3.11.1 ...
Returns a new instance of the RotatingFileHandler class. The specified file is opened and used as the stream for logging. If mode is...
Read more >
Logging - Hugging Face
logging.DEBUG (int value, 10): report all information. By default, tqdm progress bars will be displayed during model download. logging ...
Read more >
Logging — PyTorch Lightning 1.8.6 documentation
By default, Lightning uses TensorBoard logger under the hood, and stores the logs to a ... Lightning auto-determines the correct logging mode for...
Read more >
Customizing Logging Behavior While Debugging
sudo log config --mode "level:debug" --subsystem com.your_company. ... are stored in memory and then saved to the data store, or stored in memory...
Read more >
Python Logging Guide - Best Practices and Hands-on Examples
DEBUG to log detailed information, typically of interest only when diagnosing ... Unlike handlers, the logging library only provides a basic ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found