PHP中的Singleton vs Static Logger类(Laravel项目)

我有一个PHP Laravel项目,我想创建一个记录我数据库中某些用户活动的类 (一些是由用户阅读,一些是由系统分析)。 </ p>

我想知道有关这方面的最佳做法/方法。 到目前为止,经过一些推理,我确定创建一个(传统的)Eloquent模型是低效的,因为为了调用方法,每次我想记录一些活动时,我都必须从我的Logger类中实例化一个对象。 </ p>

我想到的解决方案之一是创建一个使用”枚举“模拟(抽象/最终类将包含一组常量)。 然后,从那里创建将调用Laravel的DB(静态)façade并使用“enums”类中的常量来存储日志的方法。</ p>

另一种方法是创建“Singleton” “如所以,然后再次实施数据库外观。</ p>

我想知道这些方法是否有效,方法是我不会真正实例化一个新对象并使我的内存充满了记录器(因为我打算做一些繁重的记录),以及 我每次使用记录器时都会尽可能高效。 我对其他解决方案持开放态度,例如在MySQL或其他解决方案中本地检索/存储枚举(只要它对大量日志记录有效)</ p>

非常感谢您的帮助 提前。</ p>

干杯!</ p>
</ div>

展开原文

原文

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!

douxiangui5011
douxiangui5011 请参见照明/日志/写入器
接近 4 年之前 回复
doubinduo3364
doubinduo3364 我认为@kyle是对的。IIRCilluminate.log是您可以监听的事件,您可以在此时将日志条目插入数据库。您不必引入任何单例或静态类来处理逻辑。
接近 4 年之前 回复
douyun3887
douyun3887 谢谢你的提示。我已经考虑过了,但是我正在寻找一个快速,高容量的解决方案,例如静态/单例类,在系统中有很多东西需要实现。这些可以在以后由事件替换,这听起来是正确的!
接近 4 年之前 回复
dtj88302
dtj88302 我想你最终会想要使用这个laravel.com/docs/5.3/events的事件
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐