php - Add global logs in app - what about SOLID -


i have php app, isn't compilant solid principles, whole team tries refactore code on changes. must add global logs (stored in 1 of databases), saved on creations on updates on models. models don't use orm. first solution: create static logger , call after operation on model:

public function save(objectentity $entity) {     // code prepare entity     $this->insert($entity);      logger::savelog('object has been saved');      // or maybe better - separate classes logs interface      logger::save(new logobjectentitysave()); } 

but... correct? think not compilant solid , not make new mess in current. should add - on models, or maybe on controllers after calling model saving:

public function saveaction() {     // controller code here      $model->save($objectentity);     logger::save(new logobjectentitysave()); } 

but there question: 1 action save , update data in model (i add new element when don't have existing id)? if/else , 2 log classes.. still looks bad. no idea how should right.

1) perform logging actions in model save() not in controllers, in saveaction. else going have find every $model->save($objectentity) piece of code , add logging. if forget 1 of them, logging feature not reliable, logs lie you, cannot trust them, become useless.

2) if think violate s in solid because model save() action 2 things (the insert() , savelog), no not. because delegates responsibility perform logging actions savelog(). calling savelog() fine , not violate solid.

3) static logger class indeed not best choice (cannot replaced implementation, hardcoded everywhere ...) if application not have dependency injection abilities such container, not bad choice: easy use, easy control , maintain. if makes life developer easier, step forward :) .

if have dependency injection, inject logger service symfony framework example.

4) last question when have save , update, think need if/else logging case. yes, makes complex (probably wrap in private function hide complexity) logs clear , accurate. , important. logging action complex because being logged complex. there nothing can it.

hope helps


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -