PageRenderTime 61ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/Log.php

https://bitbucket.org/mihaistefan88/log-library-for-codeigniter
PHP | 104 lines | 54 code | 15 blank | 35 comment | 10 complexity | f0b8239a2f3eb3f993ec5e507f14ad3b MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter Log Class
  4. *
  5. * Simple class for advanced logging
  6. *
  7. * @package CodeIgniter
  8. * @subpackage Libraries
  9. * @category Libraries
  10. * @author Mihai Stefan
  11. * @license LGPL v3
  12. * @link https://bitbucket.org/mihaistefan88/log-library-for-codeigniter/
  13. * @version 0.1
  14. */
  15. class Log {
  16. private $actions;
  17. private $dbtable;
  18. private $db;
  19. private $ci;
  20. private $file;
  21. private $filename;
  22. private $logdir;
  23. private $fh;
  24. /**
  25. * Constructor function
  26. *
  27. * Function which configures the instance of Log class
  28. *
  29. * @param array $config array with the config options
  30. * @return void
  31. *
  32. */
  33. public function __construct($config_new = false){
  34. //instantiate CI
  35. $this->ci = &get_instance();
  36. //default options
  37. $this->actions = array(
  38. 'login','upload','download','modif_perms','mkdir','mkfile','rmdir','rmfile',
  39. 'copyfile','movefile','adduser','edituser','rmuser','addrole','editrole','rmrole',
  40. 'editsettings','addcompany','editcompany','rmcompany','adddepartament','editdepartament',
  41. 'rmdepartament','activatecompany','deactivatecompany','view'
  42. );
  43. $this->dbtable = 'logs';
  44. $this->db = false;
  45. $this->file = true;
  46. $this->filename = 'dms_logs_'.date('Y-m-d').'.log.txt';
  47. $this->logdir = FCPATH.'logs/';
  48. //setting options
  49. if ($config_new) foreach ($config_new as $key=>$value) {
  50. if ($value != '')
  51. $this->{$key} = $value;
  52. }
  53. if ($this->file) {
  54. if (!is_dir($this->logdir)) {
  55. @mkdir($this->logdir.'/',0777);
  56. }
  57. $this->fh = fopen($this->logdir.$this->filename,'a');
  58. }
  59. }
  60. /**
  61. * Message function
  62. *
  63. * Function which inserts the message into the log file or into the database
  64. *
  65. * @param array $action action for which the message is inserted
  66. * @param array $params parameters to replace in message
  67. * @return boolean
  68. *
  69. */
  70. public function message($action, $params, $message = false) {
  71. if (!$message)
  72. $mesaj = vsprintf(lang('log_'.$action),$params);
  73. else
  74. $mesaj = vsprintf($message,$params);
  75. if ($this->file) {
  76. fwrite($this->fh, date('Y-m-d H:i:s') . ' -> ' . $mesaj . "\r\n");
  77. }
  78. if ($this->db) {
  79. $data['params'] = json_encode($params);
  80. $data['mesaj'] = $mesaj;
  81. $data['data'] = date('Y-m-d H:i:s');
  82. if ($this->ci->session->userdata('user')) $data['id_user'] = $this->ci->session->userdata('user')->id;
  83. $data['ip'] = $_SERVER['REMOTE_ADDR'];
  84. $this->ci->db->insert($this->dbtable, $data);
  85. return true;
  86. }
  87. return false;
  88. }
  89. }