Tnlog. py
# ! /Usr/bin/ENV Python # -*-Coding: UTF-8 -*- Import OS Import Sys Import Time Import Logging Import Inspect ''' Import inspectdef test (): A = inspect. stack () [1] print a To Get A tuples, such as: (<frame object at 0x8604aa4>, 'test. PY ', 10, 'function _ one', [' \ t \ tprint get_current_function_name () \ n'], 0) A = inspect. stack () [1] rowCodeThe runtime of the function (called function) Stack is 0. According to the call sequence, the swap offset value of the function stack on the call chain increases by 1.
A-> B-> C (A calls B, B calls C) C obtains the stack sequence set through inspect. For C
The stack direction of function B is inspect. Stack () [1], and function a's stack direction is inspect. Stack () [2]
The tuples are: (the caller's stack object, caller's file name, number of called lines, caller's function name, call code, 0) The last 0 is unknown. ''' Class Logger (object ): Def Printfnow (Self ): Return Time. strftime ( ' % Y-% m-% d % H: % m: % s ' , Time. localtime ()) Def _ Init __ (Self): Self. _ Logger = Logging. getlogger () path = OS. Path. abspath ( " // Tmp/TNLOG-error.log " ) Handler = Logging. filehandler (PATH) self. _ Logger . Addhandler (handler) self. _ Logger . Setlevel (logging. notset) Def Getlogmessage (self, level, message ): # Message = "[% s] % s" % (self. printfnow (), message) Frame, filename, lineno, functionname, code, unknowfield = Inspect. Stack () [2 ] ''' Log format: [time] [type] [record code] Information ''' Return " [% S] [% s] [% s-% s] % s " % (Self. printfnow (), level, filename, lineno, functionname, message) Def Info (self, message): Message = Self. getlogmessage ( " Info " , Message) self. _ Logger . Info (Message) Def Error (self, message): Message = Self. getlogmessage ( " Error " , Message) self. _ Logger . Error (Message) Def Warning (self, message): Message = Self. getlogmessage ( " Warning " , Message) self. _ Logger . Warning (Message) Def Debug (self, message): Message = Self. getlogmessage ( " Debug " , Message) self. _ Logger . Debug (Message) Def Critical (self, message): Message = Self. getlogmessage ( " Critical " , Message) self. _ Logger . Critical (Message) # Logger = consolelog () If _ Name __ = " _ Main __ " : Logger = Logger () logger.info ( " Hello " )
Main. py
ImportSysImportTnlogClassX:DefTest (Self): Logger=Tnlog. Logger () logger.info ("Hello")If _ Name __="_ Main __": X=X () X. Test ()
In addition, the library that can read the stack producer structure in python isTracebackModule. However, to use this module, you must first trigger an exception.
Zhenzhai beasts 14:48:47
How can I know the called function? Who calls me (call my function, its module name, file name, function name, resulting in the number of lines of the called Statement)
Zhenzhai beasts 14:48:55
In Python
Salary-Beijing 16:28:57
>>> Import inspect
>>> Def F1 (): F2 ()
...
>>> Def F2 ():
... Print '% s, % s' % (inspect. Stack () [0] [3], inspect. Stack () [1] [3])
...
>>> F1 ()
F2, F1
Wenwu pm Beijing 16:30:36
Positive Solution
Wenwu pm Beijing 16:30:42
It seems that everyone is playing python.
Gz_change #16:31:25
What is Python like?
Wenwu pm Beijing 16:31:39
Of course.
Wenwu pm Beijing 16:31:45
Python is really efficient.
Gz_change #16:31:53
Saw hackers and painters yesterday
Gz_change #16:32:14
Dynamic Language, it's really nice to use
Zhenzhai beasts 17:38:33
I got it.
Gz_change #17:39:08
Shenma?
Zhenzhai beasts 17:39:24
Http://www.cnblogs.com/kill-signal/archive/2012/08/14/2638255.html
Zhenzhai beasts 17:39:36
Well written.
Zhenzhai beasts 17:39:56
I don't think there is a certain number of logs.
Zhenzhai beasts 17:40:07
Debugging is too difficult.
Gz_change #17:40:46
What did you write in this blog?
Zhenzhai beasts 17:40:50
API, and even all functions can write down the parameter values at the entry.
Zhenzhai beasts 17:41:32
Troubleshooting in the future is too helpful.
Zhenzhai beasts 17:42:35
I want to explain this simple thing.
Salary-Beijing 17:42:56
Well, it's good. It's worth learning.
Gz_change #17:43:05
I have done this too.
Gz_change #17:43:24
Previously, I wrote a single-chip microcomputer data storage management module.
Gz_change #17:43:35
However, they are all manually written printing functions.
Gz_change #17:43:37
Printf
Zhenzhai beasts 17:43:42
None of these logs,ProgramTMD is a Black Box
Gz_change #17:44:09
You know
Gz_change #17:44:17
Those single-chip computers do not have screens
Gz_change #17:44:29
Debug the program, that is, burn the program
Gz_change #17:44:37
Connect to the string line
Zhenzhai beasts 17:44:37
Can you write a text file?
Gz_change #17:44:43
No
Zhenzhai beasts 17:44:49
To seal a layer of log
Gz_change #17:44:54
Because the capacity is only K
Gz_change #17:45:03
Moreover, the file cannot be copied.
Zhenzhai beasts 17:45:04
Easy scalability
Gz_change #17:45:08
Only programs can be burned in.
Gz_change #17:45:15
Therefore, you can only rely on printf
Gz_change #17:45:28
Print to serial port
Zhenzhai beasts 17:45:28
In this case, printf can accept
Gz_change #17:45:35
View printf on a PC
Gz_change #17:45:47
Data
Gz_change #17:45:59
It's so painful.
Gz_change #17:46:02
Go
Zhenzhai beasts 17:46:39
Wu Zong, who of you recommends a log analysis tool?
Zhenzhai beasts 17:47:47
The entire system should be distributed all over log
Zhenzhai beasts 17:48:11
In this way, the state of the program is known and easy to troubleshoot.
Zhenzhai beasts 17:48:24
Finally, we need a switch.
Zhenzhai beasts 17:48:53
Less logs can be written after the system is stable.