2014-12-22 3 views
0

여러 모듈에서 사용할 수있는 로거를 작성하고 싶습니다. 한 곳에서 활성화 및 비활성화 할 수 있어야합니다. 그리고 재사용이 가능해야합니다.여러 모듈에 파이썬 로그인하기

다음은 시나리오입니다. switch_module.py

class Brocade(object): 
    def __init__(self, ip, username, password): 
     ... 

    def connect(self): 
     ... 
    def disconnect(self): 
     ... 
    def switch_show(self): 
     ... 

switch_module_library.py

import switch_module 

class Keyword_Mapper(object): 
    def __init__(self, keyword_to_execute): 
     self._brocade_object = switch_module.Brocade(ip, username, password) 
     ... 
    def map_keyword_to_command(self) 
     ... 

application_gui.py

class GUI: 
    # I can open a file containing keyword for brocade switch 
    # in this GUI in a tab and tree widget(it uses PyQt which I don't know) 
    # Each tab is a QThread and there could be multiple tabs 

    # Each tab is accompanied by an execute button. 
    # On pressing exeute it will read the string/keywords from the file 
    # and create an object of keyword_Mapper class and call 
    # map_key_word_to_command method, execute the command on brocade 
    # switch and log the results. Current I am logging the result 
    # only from the Keyword_Mapper class. 

내가 가진 문제는 의지 활성화 및 비활성화 할 수있는 로거를 작성하는 방법이며, 세 개의 모듈 모두에서 하나의 파일과 콘솔에 로깅해야합니다.

나는 세 가지 모듈 에서 가져 오기 다음 INT 평 글로벌 로거를 작성하고 시도가 동일한 파일에 로그인 할 수 있도록 공통의 이름을했지만, 다중 있기 때문에 다음 문제에 달렸다 -threading 그리고 나중에 에 logger를 생성하여 그 이름에 thread-id가있는 파일에 로그를 남기므로 각 로그마다 스레드 당 을 가질 수 있습니다.

같은 파일이 아닌 다른 파일에 로그해야하는 경우 어떻게해야합니까?

필자는 파이썬 로그 기록 문서를 읽었지만 재사용 할 수있는 적절한 로깅 시스템을 작성하는 것에 대해 명확한 그림을 얻을 수 없습니다. .

I 인해 PyQt는 내가 여기에 로깅 주위에 내 머리를 얻을 수 없습니다입니다 멀티 스레딩을 사용하여 나 아닌 다른 사람에 의해 생성 된 GUI이 링크 Is it better to use root logger or named logger in Python

하지만 겪었습니다.

답변

1

나는 루트 로거 만 사용한다. (좋을지라도 이름이 붙은 로거를 만들 시간이 없다.)

내가 빨리 로거를 설정하는 함수를 만들어 : 명명 된 로거를 사용하지 않을 경우 그래서, 여기에 빠른 해결책이다

import logging 

def initLogger(level=logging.DEBUG): 
    if level == logging.DEBUG: 
     # Display more stuff when in a debug mode 
     logging.basicConfig(
      format='%(levelname)s-%(module)s:%(lineno)d-%(funcName)s: %(message)s', 
      level=level) 
    else: 
     # Display less stuff for info mode 
     logging.basicConfig(format='%(levelname)s: %(message)s', level=level) 

내가 그것을 위해 패키지를 만들어 내가 할 수 있도록 어디든지 가져올 수 있습니다.

그런 다음 내 최상위 수준에서 내가 가진 : 나는 해당 문을 사용하여, 디버깅 여부입니다 경우

import LoggingTools 
if __name__ == '__main__': 
    # Configure logger 
    LoggingTools.initLogger(logging.DEBUG) 
    #LoggingTools.initLogger(logging.INFO) 

가 따라.

import logging 
class MyClass(): 
    def __init__(self): 
     logging.debug("Debug message") 
     logging.info("Info message") 
:

그런 다음 서로의 파일에, 난 그냥 로깅을 사용

관련 문제