log - Providing easy logging to your project
- Purpose:
This is a small class module designed as a central log file creator.
The calling program is responsible for passing the proper arguments to create the log file header. For example:
(printheader=True, headertext='some,header,text,here')
It is suggested to initialise the
Logclass at program startup, with theprintheaderparameter set toTrue. This is safe because if the log file already exists, the header will not be re-created. However, if the log file does not exist, it will be created with a header, using the value of theheadertextparameter.- Platform:
Linux/Windows | Python 3.7+
- Developer:
J Berendt
- Email:
- Comments:
n/a
- Example:
To use the
Logclass in your project:>>> from utils4.log import Log >> header = 'COMMENT,TYPE,VAL,UNIT' >> logger = Log(filepath='/tmp/testlog.log', printheader=True, headertext=header) >> logger.write(text='Most cows can jump over the moon,Fact,94.2,pct')
- class log.Log(filepath, *, autofill=True, printheader=False, headertext='', sep=',')[source]
This is a small and simple log file creator/writer class.
The calling program is responsible for passing the proper arguments to create the log file header. For example:
(printheader=True, headertext='some,header,text,here')
On class instantiation, validation is performed to determine if the log file exists. If the log file does not exist, a header is required before writing to the log file. These parameters can be passed to the class on instantiation, and will be ignored if the log file already exists.
- Parameters:
filepath (str) – Full path to the log file.
autofill (bool, optional) – Automatically populate
datetime.now(), host and username values, to each log entry. Defaults to True.printheader (bool, optional) –
Print a log file header using the text passed into the
headertextparameter. Defaults to False.Note
The header will only be written if the log file does not exist.
headertext (str, optional) – String of delimited column labels to be written as the header. Defaults to ‘’.
sep (str, optional) – Separator to be used in the log file. This separator is used when writing the autofill values. Defaults to ‘,’.
- File Validation:
On class instantiation, tests are performed to ensure the log file is being populated in a logical way.
If
printheaderisFalse, and the log file does not exist, the user is notified.If
printheaderisTrue, yetheadertextis blank, the user is instructed to pass header text.If
printheaderisTrue, yet the log file already exists, the header will not be written.
- Example:
To use the
Logclass in your project:>>> from utils4.log import Log >>> header = 'COMMENT,TYPE,VAL,UNIT' >>> logger = Log(filepath='/tmp/testlog.log', printheader=True, headertext=header) >>> logger.write(text='Most cows can jump over the moon,Fact,94.2,pct')
- __init__(filepath, *, autofill=True, printheader=False, headertext='', sep=',')[source]
Log class initialiser.
- write(text: str)[source]
Write text to the log file defined at instantiation.
- Parameters:
text (str) – Delimited text string to be written to the log.
Note
If
autofillisTrue, the datetime, host and username values will be populated automatically; these do not need to be passed into thetextargument.- Design:
If the
autofillargument isTrue, the current datetime, host and username values are written (in that order), ahead of the text string provided to thetextargument. Thesepparameter (defined at instantiation), is used to separate these auto-populated fields.- Example:
To write to the log file:
>>> from utils4.log import Log >>> logger = Log(filepath='/tmp/testlog.log, autofill=True) >>> logger.write(text='Just adding some random text to my log')
- write_blank_line()[source]
Write a blank line to the log file.
Note
The
autofillinformation is not written. This is a true blank line, created by writing the system’s native line separator character(s)to the log.- Example:
To write a blank line to the log file:
>>> from utils4.log import Log >>> logger = Log(filepath='/tmp/testlog.log', autofill=True) >>> logger.write_blank_line()