Runtime log lookup / search for past events

HI there,

As part of my Phoenix application, I am keeping track of actions that my user takes in the room channel. Part of my requirements is that in the future, I can go back through my custom log and see if a specific action has taken place.

So I started out writing a simple logging system (at the time I had no knowledge of elixir’s Logger module). And this is the direction I have headed in…

A simple struct to represent a LogEntry

defmodule MyApp.Logging.LogEntry do
  defstruct [:message, :time]
end

A Loggable behaviour that is used by any of my Modules that need to be logged

defmodule MyApp.Logging.Loggable do
  @callback to_log_line(any) :: %MyApp.Logging.LogEntry{}
end

And here is my implementation of that on a module I have that I am logging

defmodule MyApp.Actions.SwingSword do

  @behaviour MyApp.Logging.Loggable

  def execute(params) do
    #relevant code to process the action
  end

  def to_log_line(action) do
    %MyApp.Logging.LogEntry{
      message: "Sword was swung",
      time: DateTime.utc_now |> DateTime.to_unix
    }
  end
end

First off, being a noob to Elixir, how does my approach look to those of you who are beyon my beginner level?

So what are my plans for all these MyApp.Logging.LogEntry objects that will be generated while the app runs? My initial plan was to just keep them in a List. But then looking up whether a specific action took place would be a bit inefficient (I think) as I would need to iterate the list to find out whether a specific LogEntry has occured.

So I have thrown around the idea of maps and keyword lists, but I am not sure of which direction to head in for handling this type of behaviour. The types of lookups that I might be wanting to do would be:

  • Was the sword swung?
  • Was the sword swung with a particular amount of force?

As part of my prototyping, I went to create a simple elixir app called “loggable” only to get the message that a module “Loggable” already exists. Which then led me to the discovery of the Loggabl module in Elixir.

While my reading of the docs suggests that it might not exactly be what I am after - would there be anything that I am missing with my basic Elixir knowledge that would allow me to bend the existing Loggable module in the direction I am wanting to take? If not, can anyone suggest the best way to do a lookup on the type of lookups exampled above?

Cheers…

1 Like

After doing some more reading about the Logger module, a custom backend looks like my starting point and storing metadata for each log entry, however, I don’t think its going to allow me to search against previous log entries.

In fact, thinking about the architecture of my application a little more, I might revise how I am checking whether an action has taken place.

Anyway, I am still open to suggestions if anyone has done something similar…

1 Like