What's the best way to log the file name and line number when using an IDE like Eclipse or Pycharm? Specifically it would be useful to link from the log statement to the source code, a very common setup for Java.
Asked
Active
Viewed 284 times
-2
-
Please note there is a similar more generic question that doesn't address logging in PyCharm and Eclipse using PyDev and in particular, says nothing about being able to link the log output to source code. A previous question that does contain such information is too specifically worded, drawing155 views over two years, vs 45,000 over ten years, but the high traffic restricting additional info. Also one survey (admittedly by JetBrains) says PyCharm is used by 35% of Python devs (only 2% Eclipse pydev) https://www.jetbrains.com/research/python-developers-survey-2018/ – chars Jan 02 '20 at 22:20
-
But this contains *none* of that context. Your posts here have no content or problem specific to an IDE/plugin. Maybe if you started the question with the basic format from the dupe, outlined a specific problem with it (that the links aren't clickable in the logs, I'm guessing?), then put an answer that explains how it works and the limitations, that might be useful. But a generic question and an answer that sends you elsewhere for any useful information isn't helpful (close to https://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is-an-answer-not-an-answer). – jonrsharpe Jan 03 '20 at 08:23
-
One could not be more specific than starting a sentence, as I have done in this question, with the words "Specifically it would be useful to link ..." Therefore it would be difficult to edit to provide more specificity. The question title itself also specifies that the query is directed at Eclipse and Pycharm users writing Python. That is the context, repeated in the description of the issue. For the 65% of python devs as cited by the poll linked in first comment, this information is useless. Nevertheless, I should give some hint at the generic answer. – chars Jan 03 '20 at 21:09
-
To note, the answer given here is complete, self-contained, simple, and succinct, providing very useful information, without need to delve further into the history of this solution by following the additional link. – chars Jan 03 '20 at 21:14
-
The other, simpler option is to add this to the dupe with a prefix like "if you want a link to the source in pydev/whatever you can use..." – jonrsharpe Jan 03 '20 at 21:17
1 Answers
-1
import logging, sys, os
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format='%(message)s | \'%(name)s:%(lineno)s\'')
log = logging.getLogger(os.path.basename(__file__))
log.debug("hello logging linked to source")
See also Pydev source file hyperlinks in Eclipse console for longer discussion and history.

chars
- 343
- 3
- 10
-
this doesn't work for me. `%(name)s` is the module name, not the filename – Aaron F Jan 21 '21 at 22:29
-
It works in Eclipse in the simple case of a self contained file with the code above. No warranty provided for all use cases, but it does answer the question, "How to log source file name ... " leaving a link to the log statement in the source code, output in the IDE console. Is there any other answer that even does that? There's also other versions under 'longer discussion and history' Why anyone leaves a comment and down votes immediately defies comprehension. It also shows a lack of character. Stackoverflow says this comment has more than 20 characters left myself. – chars Jan 25 '21 at 10:07
-
If you go here, https://docs.python.org/3/howto/logging.html it gives an example of passing the module name. The code above passes the `__file__` You need to clarify what you're doing, because it can't be implementing the above code. – chars Jan 25 '21 at 10:29
-
Thanks for your reply. After commenting I spent some time getting it working and what works for me is the format string `Formatter('%(asctime)s %(levelname)s File "%(pathname)s", line %(lineno)s:\t%(message)s')`. The problem with that is that `%(pathname)s` is very long. What works sometimes is `Formatter('%(asctime)s %(levelname)s [%(filename)s":%(lineno)s]:\t%(message)s')` - using this one the running file is sometimes hyperlinked but other files in the project aren't. – Aaron F Jan 25 '21 at 11:31
-
Now I read your last comment I realise that you're passing the filename to the logger in place of the module name. Why not use `%(filename)s` instead of changing the name? – Aaron F Jan 25 '21 at 11:35
-
(to clarify, what I mean by "works" in my comment is "Pycharm turns into hyperlinks" - I originally came here from the question you linked in this answer. I meant to write the original comment on your answer in the other question, but it looks like I got confused :-) ) – Aaron F Jan 25 '21 at 11:38