Logging file logs and logging logs
1. Example
Import logginglogging.basicConfig(filename='log.txt ', # file name level = logging. DEBUG, # level format = U' time: % (asctime) s \ n Level: % (levelname) s \ n message: % (message) s \ n ', # log format datefmt = '% Y-% m-% d % H: % M: % s') # Time Format logging. debug (u'first record ') logging.info (u'second record ')
2. Level
Messages recorded in logs can be divided into different levels. The following predefined levels are generally used.
Each level has a corresponding value, which can be used to compare the level.
- Logging. CRITICAL50
- Logging. ERROR40
- Logging. WARNING30
- Logging. INFO20
- Logging. DEBUG10
- Logging. NOTSET0
Each level has a corresponding method. It uses lowercase letters, such as logging. debug () and logging.info (), to record messages at the DEBUG and INFO levels respectively.
The level configured in logging. basicConfig can be used to filter messages. messages with a lower configuration level will be ignored and will not be written to files.
For example, if level = logging. INFO is configured at the beginning, messages that call logging. debug () will be ignored and will not be recorded in files. Only info (), warning (), and higher levels are recorded.
3. log format
The following parameters are supported for formatting strings:
- % (Asctime) sHuman-readable time when the LogRecord was created. By default this is of the form '2017-07-08 16:49:45, 2003
- % (Created) fTime when the LogRecord was created (as returned by time. time ()).
- % (Filename) sFilename portion of pathname.
- % (FuncName) sName of function containing the logging call.
- % (Levelname) sText logging level for the message ('debug', 'info', 'warning', 'error', 'critical ').
- % (Levelno) sNumeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL ).
- % (Lineno) dSource line number where the logging call was issued (if available ).
- % (Module) sModule (name portion of filename ).
- % (Msecs) dMillisecond portion of the time when the LogRecord was created.
- % (Message) sThe logged message
- % (Name) sName of the logger used to log the call.
- % (Pathname) sFull pathname of the source file where the logging call was issued (if available ).
- % (Process) dProcess ID (if available ).
- % (ProcessName) sProcess name (if available ).
- % (RelativeCreated) dTime in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.
- % (Thread) dThread ID (if available ).
- % (ThreadName) sThread name (if available ).
3. Time Format
Time Format String and time. strftime () use the same Parameter
- %Locale's abbreviated weekday name.
- %Locale's full weekday name.
- % BLocale's abbreviated month name.
- % BLocale's full month name.
- % CLocale's appropriate date and time representation.
- % DDay of the month as a decimal number [01,31].
- % HHour (24-hour clock) as a decimal number [00,23].
- % IHour (12-hour clock) as a decimal number [01,12].
- % JDay of the year as a decimal number [001,366].
- % MMonth as a decimal number [01,12].
- % MMinute as a decimal number [].
- % PLocale's equivalent of either AM or PM.
- % SSecond as a decimal number [].
- % UWeek number of the year (Sunday as the first day of the week) as a decimal number []. all days in a new year preceding the first Sunday are considered to be in week 0.
- % WWeekday as a decimal number [0 (Sunday), 6].
- % WWeek number of the year (Monday as the first day of the week) as a decimal number []. all days in a new year preceding the first Monday are considered to be in week 0.
- % XLocale's appropriate date representation.
- % XLocale's appropriate time representation.
- % YYear without century as a decimal number [].
- % YYear with century as a decimal number.
- % ZTime zone name (no characters if no time zone exists ).
- %A literal '%' character.
Where is the default log file stored in the logging module of python?
When configuring log file objects, you need to fill in the location of the log file. I put them all according to project requirements.
No research is performed by default.
How to configure a log file for a project
You can create a log file and make it contain diagnostic information related to interoperability, program loading, and network. You can enable logging by setting the registry key. First, set a registry key to enable regular logging, and then set the registry key for the required logging components and options.
You can set the registry key using the following methods:
Use the Remote Registry Editor in Visual Studio.
In. NET Compact Framework 2.0 Service Pack 1, use the logging option in the remote Performance Monitor. For more information about remote performance monitoring, see how to monitor performance at runtime.
In. NET Compact Framework 3.5, you can use the logging tool NetCFLogging.exe, which provides a simple graphical user interface for enabling and disabling logging. This tool is included in Power Toys for. NET Compact Framework. For more information, see Power Toys for. NET Compact Framework.
Use the Registry and RegistryKey classes. the. NET Compact Framework 2.0 and later versions support these classes.
The following table summarizes these log files.
Logging Component
Log File Content
Interoperability
Records COM interoperability calls. Provides information about platform calling and sending.
Error
Records all unprocessed and local exceptions. Record errors to log files and OutputDebugString. A log file is created for each Assembly located in the current path and applied to the current session. The log file will be overwritten after the first unhandled exception or local exception occurs.
Load programs
Record information about program loading. The file header contains the following information:
Application name.
Process ID (provided by Windows Embedded CE ).
The local date and time when the log file was created. The format is not global, but regional-specific.
. NET Compact Framework version, for example, 2.0.5021.00.
Platform-related information, such as Windows Embedded CE v5.0.1400 (CEPC) WinCE5x86 debug Dev i386 IJITv2.
The file provides the following information:
Force status (compatibility mode ).
The trust level specified for the module when the module is loaded.
Failed to parse the method.
Failed to parse the type.
An error occurred while searching for or loading the assembly or module.
Assembly loaded successfully.
The metadata version is invalid.
An error occurred while searching for the platform to call the DLL.
An error occurred while searching for functions in the platform Call DLL.
Policy file name, or the fact that the file is missing.
Major Errors During policy file processing.
Managed Assembly policy-based redirection.
You can also include information about global assembly caching.
Network
Record network traffic. The network log file is a binary file. If no. NET Compact Framework Log Viewer Logviewer.exe is available, the file cannot be accessed. In. NET Compact Framework 3.5 and later versions, the Log Viewer is included in Power Toys for. NET Compact Framework. For more information, see Power Toys for. NET Compact Framework.
Because network logging occurs on the Windows Socket Layer, the log file only contains network packet information. This includes data sent over the network, some of which may be sensitive data and therefore need to be encrypted.
Terminator
Records the class names of objects not released before the Garbage Collector discards them .. NET Compact Framework 3... the remaining full text>