You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Action.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\AdminAudit\Actions;
  27. use OCP\ILogger;
  28. class Action {
  29. /** @var ILogger */
  30. private $logger;
  31. /**
  32. * @param ILogger $logger
  33. */
  34. public function __construct(ILogger $logger) {
  35. $this->logger = $logger;
  36. }
  37. /**
  38. * Log a single action with a log level of info
  39. *
  40. * @param string $text
  41. * @param array $params
  42. * @param array $elements
  43. * @param bool $obfuscateParameters
  44. */
  45. public function log(string $text,
  46. array $params,
  47. array $elements,
  48. bool $obfuscateParameters = false) {
  49. foreach($elements as $element) {
  50. if(!isset($params[$element])) {
  51. if ($obfuscateParameters) {
  52. $this->logger->critical(
  53. '$params["'.$element.'"] was missing.',
  54. ['app' => 'admin_audit']
  55. );
  56. } else {
  57. $this->logger->critical(
  58. sprintf(
  59. '$params["'.$element.'"] was missing. Transferred value: %s',
  60. print_r($params, true)
  61. ),
  62. ['app' => 'admin_audit']
  63. );
  64. }
  65. return;
  66. }
  67. }
  68. $replaceArray = [];
  69. foreach($elements as $element) {
  70. if($params[$element] instanceof \DateTime) {
  71. $params[$element] = $params[$element]->format('Y-m-d H:i:s');
  72. }
  73. $replaceArray[] = $params[$element];
  74. }
  75. $this->logger->info(
  76. vsprintf(
  77. $text,
  78. $replaceArray
  79. ),
  80. [
  81. 'app' => 'admin_audit'
  82. ]
  83. );
  84. }
  85. }