최진우.
최진우 블로그
최진우.
전체 방문자
19,683
오늘
9
어제
5
  • Category
    • Log
      • essay
      • review
      • travel
      • english
    • Dev
      • javascript
      • react
      • go
      • css
      • linux
      • python
      • algorithm
      • git
      • cloud
      • develop
      • cinema4d
      • 3dsmax
      • tistory
    • Slack
    • Notion
    • VSCode

블로그 메뉴

  • About
  • Portfolio

인기 글

  • 스타트업 개발자 채용에 관한⋯
    2022.06.02
  • 대표의 역할 vs 개발자의 역할
    2022.06.02
  • 존 드라이든
    2022.05.30
  • 파이썬이 아닌 maxscript 소스
    2022.06.17
  • 유태경전
    2022.05.30

최근 댓글

  • 이후에 어떻게 되셨나요?
    ㅇㅇㅇㅇ

최근 글

  • language market fit 이란?
    2022.07.05
  • 복문이란 뭘까?
    2022.07.05
  • outline 은 border와 다르다
    2022.07.04
  • 잡지 메모
    2022.06.22
  • 깃허브 코파일럿 유료화됨
    2022.06.22
    깃허브 코파일럿 유료화됨
hELLO · Designed By pronist.
최진우.

최진우 블로그

[3dsMax python] Log
Dev/3dsmax

[3dsMax python] Log

2020. 12. 20. 14:24

Logging 모듈

print 문으로 어느 정도 로그를 남겼었지만, 스크립트를 만들어 디자이너에게 줄 경우, 로그파일이 필요해서 합니다. 

참고로 맥스에 print 문이 많이 찍히게 되면 맥스가 멈춰서 한참 기다려야합니다. (무거운 파일이면 맥스가 꺼집니다)

그래서 저는 되도록이면 주요 로그는 콘솔에, 주요 로그와 세부로그는 파일로 남겨보겠습니다.

그냥 import 해서 사용하면 됩니다. 빨간 글씨로 출력이 됩니다. 하지만 default 레벨설정이 warning이여서 info 랑 debug는 출력안됩니다. 

 

import logging

logging.debug("test")
logging.info("test")
logging.warning("test")
logging.error("test")

# WARNING:root:test
# ERROR:root:test

 

Level

로깅 모듈을 이용하면, debug, info, warning, error, critical 을 구분할수 있습니다.

각각 critical=50, error = 40, warning=30, info=20, debug=10, notset=0 으로 계층이 나눠져 있습니다.

레벨을 설정하면 그 상위 레벨만 출력이 됩니다.

그럼 파일에는 info, debug 까지 남기고, 콘솔에는 warning이랑 error만 출력해주면 될것 같습니다

 

logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
logger.debug("test")
logger.info("test")
logger.warning("test")
logger.error("test")

# INFO:simple_example:test
# WARNING:simple_example:test
# ERROR:simple_example:test

File 출력과 Console 출력

로그메세지를 파일에 출력하는것과, 콘솔에 출력하는것을 따로 설정을 해줘야합니다.

중요한것은 root logger도 level 을 반드시 설정해줘야합니다.

핸들러를 닫아주고 지워주는것도 꼭 해줘야합니다. max가 열려있는걸로 인식하면 로그가 2줄씩 4줄씩 찍히기 때문입니다.

 

 

파일에는 info, debug 까지 남기고, 스크립트 리스너에는 warning이랑 error만 출력해주는 코드입니다. 😎

 

import logging
import os

file_path = os.path.abspath(__file__)
dir_path = os.path.dirname(file_path)
file_name = os.path.basename(file_path).split('.py')[0]
logger = logging.getLogger(file_name)
logger.setLevel(logging.DEBUG)    # IMPORTANT!

sh = logging.StreamHandler()
fh = logging.FileHandler(filename=dir_path + '\\' + file_name + '.log')

sh.setLevel(logging.WARNING)
fh.setLevel(logging.DEBUG)

logger.addHandler(sh)
logger.addHandler(fh)

logger.debug("debug-")
logger.info("info-")
logger.warning("warning-")
logger.error("error-")


handlers = logger.handlers[:]
for handler in handlers:
    handler.close()
    logger.removeHandler(handler)

 

출력형식 Fomatting

출력형식이 없으니깐 너무 밋밋해서 출력형식을 추가했습니다.

 

 

코드는 핸들러를 생성하는 부분 바로 다음에 추가하면 됩니다.

예외처리는 참고자료에서 에외처리 링크를 참고하면 될것 같습니다.

 

LOG_FORMAT = '[%(asctime)-10s] (Line: %(lineno)d) %(name)s:%(levelname)s - %(message)s'
formatter = logging.Formatter(LOG_FORMAT)
sh.setFormatter(formatter)
fh.setFormatter(formatter)

참고 자료

 

Python logging setLevel not logging:

stackoverflow.com/questions/30677573/python-logging-setlevel-not-logging

 

Fomat:

greeksharifa.github.io/%ED%8C%8C%EC%9D%B4%EC%8D%AC/2019/12/13/logging/

 

Except:

wayhome25.github.io/python/2017/02/26/py-12-exception/

    'Dev/3dsmax' 카테고리의 다른 글
    • [3dsMax python] Start up
    • [3dsMax python] Menu & Macroscript with pymxs
    • [3dsMax python] GUI with pymxs
    • [3dsMax python] V-ray material with pymxs
    3dsmax, log, Logging, pymxs, python, 로그
    최진우.
    최진우.
    우당탕탕 최진우의 세상만사  😎
    댓글쓰기
    다음 글
    [3dsMax python] Menu & Macroscript with pymxs
    이전 글
    [3dsMax python] GUI with pymxs
    • 이전
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • ···
    • 12
    • 다음