python23 、 datatime 与 logging

datatime: It is a module (package) not a database. A package is the sum of classes. There are other methods in the class (this is built-in). The self-created package has no class and the method of the class is
similar to the time package. Time.time gets the current Time
is better than time, and only means time than time. Data time means more than time. Data
time.data() means a date object. Putting parentheses into parameters automatically generates a date object
datatime.time() means time object
datatime. datatime() represents the object of date and time, only write the date will add the whole point of 0 by default
datatime.timedelta() represents the time interval, enter the unit of the interval = value in parentheses,
and then enter other dates and times when printing Object + time interval The instantiated object gets the time after the interval, the way the alarm is used is related to this

You can treat the package and database as a class with their own methods. The above methods of adding classes will automatically generate objects to instantiate objects
. The data and time behind are all class names
. The method of the class is the method of the class, and the method cannot be pointed out. Properties, unless there are properties in the method, but the built-in is generally OK,

Get the current date and time: Generally, you can only use datetime.datetime.now() to import the datetime database.
If you want to use datetime.now(); you must first enter from datetime import datetime
and add now after the datetime objects in the export package. , Print the current time, utcnow prints "Greenwich Mean Time". These are class methods,
and datetime.datetime.now().date() uses two methods at the same time, depending on whether the return value of this function returns Object, if it is an object, you can continue to call the function.
Because the current class object plus now is still a class object, the class (the object) can also use methods, and the identity can be used forever.

Date and time are converted to timestamp: time and date object.timestamp();
timestamp is converted to date and time: datetime.fromtimestamp (timestamp) The
timestamp is the time (in seconds) from when Greenwich Mean Time began to count to the present

Date and time object to string: time and date object.strftime(format)
datetime.now().strftime("%Y-%m-%d") exported the datetime class in the package from datetime import datetime, you don’t need to enter two datetime
datetime.datatime.now().strftime("%Y-%m-%d")
followed by strftime("%Y-%m-%d") are all formatted into a character
string to a date and time object: datetime.strptime(data_str (time in character form), format1)
The time in character form sep-13-12 is foreign, 12 is the year at the end, so format corresponds to %b-%d-%Y (%B is a foreign month)
format1 (variable)='%Y-%m-%d %H:%M:%S' year, month and day have the corresponding letter
now. Required to extract the time

The time package, time.time() is also the timestamp he asked for.
Time.struct_time() adds parameters in parentheses. struct_time can also be formatted like datetime, converting the array into the time format
time.asctime() converts the date and time into The English format
time.ctime() is similar to asctime but is different.
time.gmtime() converts a timestamp to UTC time zone (0 time zone)
time.localtime() view UTC local time

logging module:
write logs
in general form: set filename and filemode in logging.basicConfig(), then only logs will be saved to a file, not output to the console.
Logging is the log module that comes with the python standard library. It uses software to record software to record problems and events to logging.
LOG_FORMAT="%(asctime)s-%(levelname)s-%(message)s"
LOG_FORMAT is capitalized because Set a constant that will not be changed

logging.basicConfig() configures the logger
logging.basicConfig(level=logging.WARNING,format=LOG_FORMAT)
format is output according to the constant format set above

logging.level(message) creates a level log,
logging.debug('asawdawdasd')
logging.info('asd')

The first entry of level=logging.WARNING corresponds to all executions starting from WARNING in the order set by logging.level(message) and going down to
logger.info("this is info") where if level=logging.info starts from info, The call of ogging.debug did not generate a log,
logger.debug("this is debug"), that is, the call level mentioned above does not exceed the default level, there will be no output;
logger.warning("this is warning") and other calls When the level exceeds the default level, the log records the call level.
logging.error("this is error") So it is only related to the level, and has nothing to do with the sorting
logger.critical("this is critical")

所以只输出
2016-09-13 18:52:37,492 INFO - this is info
2016-09-13 18:52:37,492 WARNING - this is warning
2016-09-13 18:52:37,492 ERROR - this is error
2016-09-13 18:52:37,492 CRITICAL - this is critical

%(asctime)s The time when the log event occurred
%(levelname)s The log level of the log record
%(message)s The text content of the log record
Several levels in Logging: DEBUG (debugging) <INFO (normal operation) <WARNING ( Warning)
<ERROR (error, some sections cannot be used but still run) <CRITICAL (dangerous, completely unusable)

Modular log writing:
the use of components : the use of the Logger, Handler, Filter, and Formatter classes of the logging module
Create a logger (log processor) object, __name__ is the name of the current file, getLogger sets the object name method
logger.setLevel( ) Brackets plus logging. level, set the lowest logging level of execution

Define the handler (log handler), decide where to send the log to
a=logging.StreamHandler() Define the object from the handler to the console
Set the log level (level)
a.setLevel() Brackets plus logging. Level, set the response level
StreamHandler Output to the console
FileHandler Output to the file (file name suffix. or _ plus log), add encoding=utf8 at the end of the brackets to prevent garbled characters after being stored in the file)

Set the output format of the handler Formatters (log formatter)
formatter (formatter setting object)=logging.Formatter (same as the LOG_FORMAT format above) mode=w or a, operation file mode
mode: a+ for additional log, set to w+ means every Clear it again, re-record the log to overwrite the previous log
a.setFormatter(formatter)

Add the object of the handler to the corresponding logger to
logger.addHandler(a)

To execute the above function, there must be a main function at the beginning. This is a fixed requirement.
If name = main,
you can start to indent input judgment statements and other function statements, which
means that from here on execution, your code can be written according to your needs.
The main program can be executed if the level of the main program is different from that of the component. It is not affected by the setting level of the component.
The main program that is started is the first condition for executing the code. For the clarity of the code, you can see where the code starts at a glance.

The input method is to input the
from class import class object from the console, and the imported content is the content after import

Guess you like

Origin blog.csdn.net/qwe863226687/article/details/114015513
Recommended