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
|
import logging import sys
log1 = logging.Logger("0.0")
console = logging.StreamHandler(sys.stdout) console.setLevel(logging.INFO) formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') console.setFormatter(formatter) log1.addHandler(console)
log2 = logging.Logger("-.-")
files = logging.FileHandler("log2.log", mode="a", encoding="utf-8") files.setLevel(logging.WARNING) formatter = logging.Formatter("%(name)s %(levelname)s %(message)s") files.setFormatter(formatter) log2.addHandler(files)
for i in range(1, 6): log1.log(i*10, logging.getLevelName(i*10).lower()) log2.log(i*10, logging.getLevelName(i*10).lower())
logging.critical("AHHH! I'm the root logger but you forget me!") logging.getLogger("root").info("Of course not!")
|