11<?php
2+
3+ declare (strict_types=1 );
4+
25/**
36 * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
47 * SPDX-License-Identifier: AGPL-3.0-or-later
58 */
9+
610namespace OC \Log ;
711
812use OC \SystemConfig ;
13+ use OCP \IRequest ;
14+ use OCP \Server ;
915
1016abstract class LogDetails {
1117 public function __construct (
1218 private SystemConfig $ config ,
1319 ) {
1420 }
1521
16- public function logDetails (string $ app , $ message , int $ level ): array {
22+ public function logDetails (string $ app , string | array $ message , int $ level ): array {
1723 // default to ISO8601
1824 $ format = $ this ->config ->getValue ('logdateformat ' , \DateTimeInterface::ATOM );
1925 $ logTimeZone = $ this ->config ->getValue ('logtimezone ' , 'UTC ' );
@@ -29,13 +35,13 @@ public function logDetails(string $app, $message, int $level): array {
2935 // apply timezone if $time is created from UNIX timestamp
3036 $ time ->setTimezone ($ timezone );
3137 }
32- $ request = \ OC :: $ server -> getRequest ( );
38+ $ request = Server:: get (IRequest::class );
3339 $ reqId = $ request ->getId ();
3440 $ remoteAddr = $ request ->getRemoteAddress ();
3541 // remove username/passwords from URLs before writing the to the log file
3642 $ time = $ time ->format ($ format );
3743 $ url = ($ request ->getRequestUri () !== '' ) ? $ request ->getRequestUri () : '-- ' ;
38- $ method = is_string ( $ request ->getMethod ()) ? $ request -> getMethod () : ' -- ' ;
44+ $ method = $ request ->getMethod ();
3945 if ($ this ->config ->getValue ('installed ' , false )) {
4046 $ user = \OC_User::getUser () ?: '-- ' ;
4147 } else {
@@ -46,6 +52,7 @@ public function logDetails(string $app, $message, int $level): array {
4652 $ userAgent = '-- ' ;
4753 }
4854 $ version = $ this ->config ->getValue ('version ' , '' );
55+ $ scriptName = $ request ->getScriptName ();
4956 $ entry = compact (
5057 'reqId ' ,
5158 'level ' ,
@@ -55,14 +62,19 @@ public function logDetails(string $app, $message, int $level): array {
5562 'app ' ,
5663 'method ' ,
5764 'url ' ,
65+ 'scriptName ' ,
5866 'message ' ,
5967 'userAgent ' ,
60- 'version '
68+ 'version ' ,
6169 );
6270 $ clientReqId = $ request ->getHeader ('X-Request-Id ' );
6371 if ($ clientReqId !== '' ) {
6472 $ entry ['clientReqId ' ] = $ clientReqId ;
6573 }
74+ if (\OC ::$ CLI ) {
75+ /* Only logging the command, not the parameters */
76+ $ entry ['occ_command ' ] = array_slice ($ _SERVER ['argv ' ] ?? [], 0 , 2 );
77+ }
6678
6779 if (is_array ($ message )) {
6880 // Exception messages are extracted and the exception is put into a separate field
@@ -81,7 +93,7 @@ public function logDetails(string $app, $message, int $level): array {
8193 return $ entry ;
8294 }
8395
84- public function logDetailsAsJSON (string $ app , $ message , int $ level ): string {
96+ public function logDetailsAsJSON (string $ app , string | array $ message , int $ level ): string {
8597 $ entry = $ this ->logDetails ($ app , $ message , $ level );
8698 // PHP's json_encode only accept proper UTF-8 strings, loop over all
8799 // elements to ensure that they are properly UTF-8 compliant or convert
0 commit comments