Skip to content Navigation Menu Toggle navigation Sign in Appearance settings PlatformAI CODE CREATIONGitHub CopilotWrite better code with AIGitHub Copilot appDirect agents from issue to mergeMCP RegistryNewIntegrate external toolsDEVELOPER WORKFLOWSActionsAutomate any workflowCodespacesInstant dev environmentsIssuesPlan and track workCode ReviewManage code changesAPPLICATION SECURITYGitHub Advanced SecurityFind and fix vulnerabilitiesCode securitySecure your code as you buildSecret protectionStop leaks before they startEXPLOREWhy GitHubDocumentationBlogChangelogMarketplaceView all featuresSolutionsBY COMPANY SIZEEnterprisesSmall and medium teamsStartupsNonprofitsBY USE CASEApp ModernizationDevSecOpsDevOpsCI/CDView all use casesBY INDUSTRYHealthcareFinancial servicesManufacturingGovernmentView all industriesView all solutionsResourcesEXPLORE BY TOPICAISoftware DevelopmentDevOpsSecurityView all topicsEXPLORE BY TYPECustomer storiesEvents & webinarsEbooks & reportsBusiness insightsGitHub SkillsSUPPORT & SERVICESDocumentationCustomer supportCommunity forumTrust centerPartnersView all resourcesOpen SourceCOMMUNITYGitHub SponsorsFund open source developersPROGRAMSSecurity LabMaintainer CommunityAcceleratorGitHub StarsArchive ProgramREPOSITORIESTopicsTrendingCollectionsEnterpriseENTERPRISE SOLUTIONSEnterprise platformAI-powered developer platformAVAILABLE ADD-ONSGitHub Advanced SecurityEnterprise-grade security featuresCopilot for BusinessEnterprise-grade AI featuresPremium SupportEnterprise-grade 24/7 supportPricing Search or jump to... Search code, repositories, users, issues, pull requests... Search Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Saved searches Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our documentation. Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} twtrubiks / python-notes Public Notifications You must be signed in to change notification settings Fork 52 Star 109 Code Issues 0 Pull requests 0 Actions Projects Security and quality 0 Insights Additional navigation options Code Issues Pull requests Actions Projects Security and quality Insights FilesExpand file tree masterBreadcrumbspython-notes/logging_tutorial.pyCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory67 lines (51 loc) · 1.83 KB masterBreadcrumbspython-notes/logging_tutorial.pyCopy pathTopFile metadata and controlsCodeBlame67 lines (51 loc) · 1.83 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import logging '''ref. https://docs.python.org/3/library/logging.htmlLevel DEBUG < INFO < WARNING < ERROR < CRITICAL''' def ex1(): # logging.basicConfig(level=logging.DEBUG) # will print 'warning message' , 'error message', because the default level is WARNING logging.warning('warning message') logging.error('error message') logging.debug('I told you so - debug') logging.info('I told you so - info') def ex2(): logging.error('test') log1 = logging.getLogger('my_app') log2 = logging.getLogger(__name__) log1.warning('I told you') log2.warning('warning message') def ex3(): # will print all message , because the level is DEBUG format_log = '%(asctime)s %(levelname)s:%(message)s' logging.basicConfig(filename='example.log', format=format_log, level=logging.DEBUG) # logging.basicConfig(format=format_log, level=logging.DEBUG) logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too') def ex4(): # multiple-handlers-and-formatter logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) log_file = 'my_test_logger.log' f_log = logging.FileHandler(log_file, mode='w') f_log.setLevel(logging.ERROR) # 設定 console 並且定義為 DEBUG c_log = logging.StreamHandler() c_log.setLevel(logging.DEBUG) format_log = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') f_log.setFormatter(format_log) c_log.setFormatter(format_log) logger.addHandler(f_log) logger.addHandler(c_log) logger.debug('debug message') logger.info('info message') logger.warning('warning message') logger.error('error message') logger.critical('critical message') if __name__ == "__main__": # ex1() # ex2() # ex3() ex4() You can’t perform that action at this time.