logging

Python3 add logging level

孤街浪徒 提交于 2020-01-10 14:32:04
问题 I have this code which works just fine for me. import logging import logging.handlers logger = None def create_logger(): global logger logger = logging.getLogger('Logger') logger.setLevel(logging.DEBUG) handler = logging.handlers.RotatingFileHandler("C:/Users/user/Desktop/info.log", maxBytes=1000000, backupCount=20) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) create_logger() logger.info("Text

How should I correctly handle exceptions in Python3

ぐ巨炮叔叔 提交于 2020-01-10 14:13:20
问题 I can't understand what sort of exceptions I should handle 'here and now', and what sort of exceptions I should re-raise or just don't handle here, and what to do with them later (on higher tier). For example: I wrote client/server application using python3 with ssl communication. Client is supposed to verify files on any differences on them, and if diff exists then it should send this 'updated' file to server. class BasicConnection: #blablabla def sendMessage(self, sock, url, port,

What's the overhead of creating a SLF4J loggers in static vs. non-static contexts?

China☆狼群 提交于 2020-01-10 08:45:20
问题 I've always used the following pattern to construct (SLF4J) loggers: private static final Logger log = LoggerFactory.getLogger(MyClass.class); This has worked so far, but I was wondering about the static context at some point and the need to pass in the concrete class literal all the time instead of just using a non-static logger like private final Logger log = LoggerFactory.getLogger(getClass()); This has basically been asked (and answered) before here for LOG4J Should logger be private

Should I catch exceptions only to log them?

Deadly 提交于 2020-01-10 07:15:12
问题 Should I catch exceptions for logging purposes? public foo(..) { try { ... } catch (Exception ex) { Logger.Error(ex); throw; } } If I have this in place in each of my layers (DataAccess, Business and WebService) it means the exception is logged several times. Does it make sense to do so if my layers are in separate projects and only the public interfaces have try/catch in them? Why? Why not? Is there a different approach I could use? 回答1: Definitely not. You should find the correct place to

Log4j2 - Configure RolloverStrategy to delete old log files

戏子无情 提交于 2020-01-10 05:14:08
问题 I'm trying to configure log4j to keep only specified amount of backup files or keep files that are not older than some age. Ultimately I want to have time [daily] based triggering policy and keep 30 backup files or delete files that are older then 30 days. After doing some research I learned that I can't specify max number of backup files when using time policy however I came across this issue https://issues.apache.org/jira/browse/LOG4J2-435 and this documentation fragment http://logging

Can I disable an appender in logback?

我怕爱的太早我们不能终老 提交于 2020-01-09 19:53:27
问题 Can I disable an appender in logback on the xml config? I have my configuration and I want to put two appenders, one for database and other for text logs, but only one must be activated. thanks! 回答1: Not sure why you want to deactivate an appender, what are you trying to achieve by disabling. There are some ways to achieve it Add the appender in logback.xml and keep it commented. When you like to enable it then uncomment the appender and reload logback configuration (http://logback.qos.ch

How do you save an Android application log to a file on a physical device?

大城市里の小女人 提交于 2020-01-09 10:50:36
问题 I want to know why my Android application service occasionally goes down (whether it's the OS killing it or a crasch) and therefore I want to save a log file on my phone. How can is this be done? 回答1: Basically you got two possibilities and normally the first one should help you finding already the reason for the crash without coding anything since the logcat should show up the error occurred that led to the service end. 1) Using the logcat command Having Log.i("your tag", "output text") you

Listening console.log

五迷三道 提交于 2020-01-09 10:25:07
问题 I want to set a listener for console.log() and do something with the message without preventing the default behaviour. So, the console of the dev tools should get the message as well. Any ideas? 回答1: Never tried this in a webpage, but it work in a browser plugin (where javascripts rights are are not the same for security reasons). You could definitively go for something like this : (function(){ var originallog = console.log; console.log = function(txt) { // Do really interesting stuff alert(

How to use PHP to delete X number of lines from the beginning of a text file?

浪尽此生 提交于 2020-01-09 09:28:12
问题 I'm writing a PHP script and the script outputs a simple text file log of the operations it performs. How would I use PHP to delete the first several lines from this file when it reaches a certain file size? Ideally, I would like it to keep the first two lines (date/time created and blank) and start deleting from line 3 and delete X amount of lines. I already know about the filesize() function, so I'll be using that to check the file size. Example log text: *** LOG FILE CREATED ON 2008-10-18

Avoid printStackTrace(); use a logger call instead

百般思念 提交于 2020-01-09 08:27:08
问题 In my application, I am running my code through PMD.It shows me this message: Avoid printStackTrace(); use a logger call instead. What does that mean? 回答1: It means you should use logging framework like logback or log4j and instead of printing exceptions directly: e.printStackTrace(); you should log them using this frameworks' API: log.error("Ops!", e); Logging frameworks give you a lot of flexibility, e.g. you can choose whether you want to log to console or file - or maybe skip some