14.0.5 Log when an action happens
Description:
When you schedule a program to run in an automated way, it can be useful to have that program log the fact that it ran. This let's you monitor when your programs are running so you can see a history of what happened and when. If you do this with all your automated prgram and have them save log files to a single location (such as a database table as in this example) you can use this information to build a report or dashboard that lets you easily monitor the status of all your automations.
Example:
import pandas as pd
from datetime import datetime
# Get the current timestamp to record when your action took place
start_time_now = datetime.name() # get the current datetime timestamp
action_date = start_time_now.strftime("%Y%m%d") # specify the date formating that you want
action_date = str(action_date) # convert to a string
# save stats about your action as dictionary, which will be a row to add to your Database
log_row = { 'Action_ID' : ['1']
, 'Action' : ["This is a description of what action was performed"]
, 'Action_Datetime' : [action_date]
}
# Turn your row into a DataFrame with one row
DF_log_row = pd.DataFrame(data = log_row)
# Append your DF to your Database to load that an action occured (SQLite in this example)
engine = create_engine("C:\path\to\your\database.db") # create a database connection
# append the row to the database
DF_log_row.to_sql( 'table_name'
, con = engine
, schema = 'schema_name
, index = False
, if_exists = 'append'
)