0

I have a PHP Laravel project, and I want to create a class that logs some of the user activity in my database (some is meant to be read by the user, some is to be analyzed by the system).

I wanted to know what are the best practices/approaches in regards to this. So far, after some reasoning, I determined that creating a (traditional) Eloquent Model would be inefficient, since in order to call methods, I would have to instantiate an object from my Logger class everytime I want to log some activity.

One of the solutions I am thinking of is to create a Static class that logs the activity with "enums" emulation (abstract/final class that will hold a set of constants). Then, from there create methods that will call Laravel's DB (static) façade and use the constants from the "enums" class to store the log.

The other alternative is to create a "Singleton" like so, and then implement the DB façade again.

I wanted to know if these methods are effective in the way that I won't be really instantiating a new object and cluttering my memory full of loggers (since I am planning to do some heavy logging), and that everytime I use a logger it will be as efficient as possible. I am open to other solutions, such as retrieving/storing enums natively in MySQL or some other solution (as long as it is efficient to high volumes of logging)

Many thanks for your help in advance.

Cheers!

Community
  • 1
  • 1
idelara
  • 1,786
  • 4
  • 24
  • 48
  • 3
    I think that you're going to want to end up using events for this https://laravel.com/docs/5.3/events – kyle Nov 04 '16 at 19:40
  • Thank you for the hint. I had considered it already, but there are many things to be implemented in the system that I was looking for a quick, high volume efficient solution, such as static/singleton classes. These could be replaced later on by events, which sounds about right! – idelara Nov 04 '16 at 19:44
  • I think @kyle is right. IIRC `illuminate.log` is an event that you can listen for and you can insert your log entries into your database at that point. You don't have to introduce any singleton or static classes to handle the logic. – Jeff Lambert Nov 04 '16 at 19:45
  • @JackGal See [Illuminate/Log/Writer](https://github.com/laravel/framework/blob/5.3/src/Illuminate/Log/Writer.php#L295) – Jeff Lambert Nov 04 '16 at 19:46

0 Answers0