일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- T5 논문 리뷰
- 바닥부터 배우는 강화 학습
- CNN 논문리뷰
- Multi Task Learning Objectives for Natural Language Processing 리뷰
- MMTOD
- Zero-shot Generalization in Dialog State Tracking through GenerativeQuestion Answering
- TOD 논문리뷰
- hugging face tokenizer에서 special case 추가하기
- The Natural Language Decathlon:Multitask Learning as Question Answering
- ImageNet Classification with Deep ConvolutionalNeural Networks 리뷰
- RuntimeError: DataLoader worker (pid(s) ) exited unexpectedly
- 정책기반 agent
- BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding 논문리뷰
- 뉴텝스 400
- Attention Is All You Need 리뷰
- Attention Is All You Need
- Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer 리뷰
- BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding 리뷰
- BERT 사용방법
- Multi Task Learning Objectives for Natural Language Processing
- attention 설명
- 다양한 모듈에서 log쓰기
- NLP 논문 리뷰
- UBAR: Towards Fully End-to-End Task-Oriented Dialog System with GPT-2
- Evaluate Multiwoz
- Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer
- A Neural Attention Model for Abstractive Sentence Summarization
- 길찾기
- BART 논문리뷰
- BERT란
Archives
- Today
- Total
one by one ◼◻◼◻
python logger사용하기 본문
python 에서 log를 만드는 (경험상) 가장 편리한 방법을 정리해 보았다.
왜 print()를 안쓰는가?
- print도 간단하고 좋은 방법이지만, 따로 관리하기 어렵고, 지저분하고.. 언제 남겨진 log인지 알아보기 힘든 단점이 있다!
logger를 쓰면 좋은점?
logger를사용하면 하나의 폴더안에 있는 다양한 모듈(main.py, util.py, dataset.py 등등..) 에서 하나의 logger를 사용할 수 있다.
큰 프로그램으 관리하기 쉬워진다 등등.. 여러 장점이 있다
사용방법
일단 폴더안에 아래 코드를 만든다.
log_conf.py
#/temp/log_conf.py
import logging
def init_logger():
mylogger = logging.getLogger("my")
mylogger.setLevel(logging.INFO)
file_handler = logging.FileHandler('my.log')
mylogger.addHandler(file_handler)
그리고 logger를 사용하고 싶은 폴더에다가
#/temp/main.py
from log_conf import init_logger
init_logger()
mylogger = logging.getLogger("my")
if __name__ == '__main__':
mylogger.info("this is file 1")
example()
를 추가하고, main.py를 실행해 보면 my.log에
this is file 1
이 출력되게 된다.
여러 파일에서 사용하기
여러 파일에서 사용하려면 일단 예시로 파일을 하나 만들어 준뒤
#/temp/ex2
import logging
mylogger = logging.getLogger("my")
def example():
mylogger.info("This is file 2")
main파일에서 ex2파일을 사용하도록 수정해주고
from log_conf import init_logger
import logging
from ex2 import example
init_logger()
mylogger = logging.getLogger("my")
if __name__ == '__main__':
mylogger.info("this is file 1")
example()
main.py를 실행해 보면

이렇게 서로 다른 파일이라도 로그가 잘 합쳐져서 나온다.
formatter를 지정하면 아래처럼 좀더 자세한 로그를 만들 수 있고, vscode는 .log 확장자를 가진 파일이면 보기 쉽게 색이 칠해져서 나온다!

'유용한 기억' 카테고리의 다른 글
hugging face tokenizer에서 special case 추가하기 (0) | 2022.01.03 |
---|
Comments