My web app generate log entries on hooks such as model.save
. Right now, I have a class Entry
(representing a log entry) with a string field content
to contain the text of the log entry.
Examples of such entries:
- Entry(content=
'Account 123-456-7890 spending changed to $500/day'
) - Entry(content=
'Account 123-456-7890 paused spending'
)
The main problem with this approach is that it is difficult to derive semantic value from these Entry
objects if content
is just a string field.
For example, if I want to find out how often the spending of an account is modified on average across my team, I will have to write some custom logic to find all the entries with the text 'spending changed to'
and analyze them. This is error prone because text can contain unpredictable elements such as capitalization or white spaces.
Since most of the log entries are generated programmatically, it occurred to me that I can use an enum to represent the content of the Entry class.
This means the example above can be transformed into the following:
- Entry(content_id=1, content_args=
"{ account_id: 123-456-7890, value: '500' }"
) - Entry(content_id=2, content_args=
"{ account_id: 123-456-7890 }"
)
And the Content Enum:
class Content(Enum):
1: 'Account {{ account_id }} spending changed to ${{ value }}/day'
2: 'Account {{ account_id }} paused spending'
With structured entries, performing custom analysis on this data becomes very easy.
I am still at an extremely early stage at designing this refactor. I would not be surprised if there are existing implemention of this pattern somewhere.
Rather than re-inventing the wheel, I would like to see how others do it. Are there existing plugins/tools out there that are already implementing this pattern?