Skip to content

Commit 4f65c2d

Browse files
authored
Remove homemade Log solution, add support log-viewer (#1846)
* remove anything related to logs * fix tests * User must be authenticated before we give them access * fix phpstan * use old URL + enable by default * fix secure headers * allow Notice to have their own file * improve comments
1 parent e80374f commit 4f65c2d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+553
-712
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ APP_URL=http://localhost
66
APP_FORCE_HTTPS=false
77

88
# enable or disable debug bar. By default it is disabled.
9+
# Do note that this disable CSP!!
910
DEBUGBAR_ENABLED=false
1011

12+
# enable or disable log viewer. By default it is enabled.
13+
LOG_VIEWER_ENABLED=true
14+
1115
##############################################################################
1216
# IMPORTANT: To migrate from Lychee v3 you *MUST* use the same MySQL/MariaDB #
1317
# server as v3. #

app/Actions/InstallUpdate/Pipes/AllowMigrationCheck.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace App\Actions\InstallUpdate\Pipes;
44

55
use App\Models\Configs;
6-
use App\Models\Logs;
76
use Illuminate\Support\Facades\Config;
7+
use Illuminate\Support\Facades\Log;
88

99
class AllowMigrationCheck extends AbstractUpdateInstallerPipe
1010
{
1111
public const ERROR_MSG =
1212
/* @lang text */
13-
'Update not applied: `APP_ENV` in `.env` is `production` and `force_migration_in_production` is set to `0`.';
13+
' Update not applied: `APP_ENV` in `.env` is `production` and `force_migration_in_production` is set to `0`.';
1414

1515
/**
1616
* {@inheritDoc}
@@ -24,13 +24,13 @@ public function handle(array &$output, \Closure $next): array
2424
// @codeCoverageIgnoreStart
2525
// we cannot code cov this part. APP_ENV is `testing` in testing mode.
2626
if (Configs::getValueAsBool('force_migration_in_production')) {
27-
Logs::warning(__METHOD__, __LINE__, 'Force update is production.');
27+
Log::warning(__METHOD__ . ':' . __LINE__ . ' Force update is production.');
2828

2929
return $next($output);
3030
}
3131

3232
$output[] = self::ERROR_MSG;
33-
Logs::warning(__METHOD__, __LINE__, self::ERROR_MSG);
33+
Log::warning(__METHOD__ . ':' . __LINE__ . self::ERROR_MSG);
3434

3535
return $output;
3636
// @codeCoverageIgnoreEnd

app/Actions/InstallUpdate/Pipes/ComposerCall.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use App\Facades\Helpers;
66
use App\Metadata\Versions\InstalledVersion;
77
use App\Models\Configs;
8-
use App\Models\Logs;
8+
use Illuminate\Support\Facades\Log;
99
use function Safe\chdir;
1010
use function Safe\exec;
1111
use function Safe\putenv;
@@ -28,7 +28,7 @@ public function handle(array &$output, \Closure $next): array
2828
if (Helpers::isExecAvailable()) {
2929
if (Configs::getValueAsBool('apply_composer_update')) {
3030
// @codeCoverageIgnoreStart
31-
Logs::warning(__METHOD__, __LINE__, 'Composer is called on update.');
31+
Log::warning(__METHOD__ . ':' . __LINE__ . ' Composer is called on update.');
3232

3333
// Composer\Factory::getHomeDir() method
3434
// needs COMPOSER_HOME environment variable set

app/Actions/Photo/Strategies/AddDuplicateStrategy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use App\Exceptions\ModelDBException;
66
use App\Exceptions\PhotoResyncedException;
77
use App\Exceptions\PhotoSkippedException;
8-
use App\Models\Logs;
98
use App\Models\Photo;
109
use Illuminate\Database\Eloquent\ModelNotFoundException;
10+
use Illuminate\Support\Facades\Log;
1111

1212
class AddDuplicateStrategy extends AbstractAddStrategy
1313
{
@@ -30,7 +30,7 @@ public function do(): Photo
3030
if ($this->parameters->importMode->shallResyncMetadata()) {
3131
$this->hydrateMetadata();
3232
if ($this->photo->isDirty()) {
33-
Logs::notice(__METHOD__, __LINE__, 'Updating metadata of existing photo.');
33+
Log::notice(__METHOD__ . ':' . __LINE__ . ' Updating metadata of existing photo.');
3434
$this->photo->save();
3535
$hasBeenReSynced = true;
3636
}

app/Actions/Settings/UpdateLogin.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
use App\Exceptions\ModelDBException;
99
use App\Exceptions\UnauthenticatedException;
1010
use App\Models\Configs;
11-
use App\Models\Logs;
1211
use App\Models\User;
1312
use Illuminate\Support\Facades\Auth;
1413
use Illuminate\Support\Facades\Hash;
14+
use Illuminate\Support\Facades\Log;
1515

1616
class UpdateLogin
1717
{
@@ -36,7 +36,7 @@ public function do(?string $username, string $password, string $oldPassword, str
3636
$user = Auth::user() ?? throw new UnauthenticatedException();
3737

3838
if (!Hash::check($oldPassword, $user->password)) {
39-
Logs::notice(__METHOD__, __LINE__, sprintf('User (%s) tried to change their identity from %s', $user->username, $ip));
39+
Log::channel('login')->notice(__METHOD__ . ':' . __LINE__ . sprintf('User (%s) tried to change their identity from %s', $user->username, $ip));
4040

4141
throw new UnauthenticatedException('Previous password is invalid');
4242
}
@@ -69,12 +69,12 @@ public function do(?string $username, string $password, string $oldPassword, str
6969
private function updateUsername(User &$user, string $username, string $ip): void
7070
{
7171
if (User::query()->where('username', '=', $username)->where('id', '!=', $user->id)->count() !== 0) {
72-
Logs::notice(__METHOD__, __LINE__, sprintf('User (%s) tried to change their identity to (%s) from %s', $user->username, $username, $ip));
72+
Log::channel('login')->warning(__METHOD__ . ':' . __LINE__ . sprintf('User (%s) tried to change their identity to (%s) from %s', $user->username, $username, $ip));
7373
throw new ConflictingPropertyException('Username already exists.');
7474
}
7575

7676
if ($username !== $user->username) {
77-
Logs::notice(__METHOD__, __LINE__, sprintf('User (%s) changed their identity for (%s) from %s', $user->username, $username, $ip));
77+
Log::channel('login')->notice(__METHOD__ . ':' . __LINE__ . sprintf('User (%s) changed their identity for (%s) from %s', $user->username, $username, $ip));
7878
$user->username = $username;
7979
}
8080
}

app/Console/Commands/ShowLogs.php

Lines changed: 0 additions & 118 deletions
This file was deleted.

app/Exceptions/Handler.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
use App\Exceptions\Handlers\InstallationHandler;
1111
use App\Exceptions\Handlers\MigrationHandler;
1212
use App\Exceptions\Handlers\NoEncryptionKey;
13-
use App\Models\Logs;
1413
use Illuminate\Auth\AuthenticationException;
1514
use Illuminate\Contracts\Container\BindingResolutionException;
1615
use Illuminate\Contracts\Container\Container;
1716
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
1817
use Illuminate\Http\Request;
1918
use Illuminate\Session\TokenMismatchException;
2019
use Illuminate\Support\Arr;
20+
use Illuminate\Support\Facades\Log;
2121
use Psr\Container\ContainerExceptionInterface;
2222
use Psr\Container\NotFoundExceptionInterface;
2323
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
@@ -82,7 +82,7 @@ class Handler extends ExceptionHandler
8282
PhotoSkippedException::class => SeverityType::WARNING,
8383
ImportCancelledException::class => SeverityType::NOTICE,
8484
ConfigurationException::class => SeverityType::NOTICE,
85-
LocationDecodingFailed::class => SeverityType::WARNING,
85+
LocationDecodingFailed::class => SeverityType::ERROR,
8686
];
8787

8888
/**
@@ -383,15 +383,14 @@ public function report(\Throwable $e): void
383383

384384
do {
385385
$cause = $this->findCause($e);
386-
387386
if (count($cause) === 2) {
388-
Logs::log($severity, $cause[1]->getMethodBeautified(), $cause[1]->getLine(), $e->getMessage() . '; caused by');
387+
Log::log($severity->value, $cause[1]->getMethodBeautified() . ':' . $cause[1]->getLine() . ' ' . $e->getMessage() . '; caused by');
389388
}
390389

391390
if ($e->getPrevious() !== null) {
392-
Logs::log($severity, $cause[0]->getMethodBeautified(), $cause[0]->getLine(), $e->getMessage() . '; caused by');
391+
Log::log($severity->value, $cause[0]->getMethodBeautified() . ':' . $cause[0]->getLine() . ' ' . $e->getMessage() . '; caused by');
393392
} else {
394-
Logs::log($severity, $cause[0]->getMethodBeautified(), $cause[0]->getLine(), $e->getMessage());
393+
Log::log($severity->value, $cause[0]->getMethodBeautified() . ':' . $cause[0]->getLine() . ' ' . $e->getMessage());
395394
}
396395
} while ($e = $e->getPrevious());
397396
}

app/Http/Controllers/Administration/LogController.php

Lines changed: 0 additions & 71 deletions
This file was deleted.

app/Http/Controllers/SessionController.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
use App\Http\Requests\Session\LoginRequest;
1212
use App\Http\Resources\InitResource;
1313
use App\Legacy\AdminAuthentication;
14-
use App\Models\Logs;
1514
use Illuminate\Contracts\Container\BindingResolutionException;
1615
use Illuminate\Routing\Controller;
1716
use Illuminate\Support\Facades\Auth;
17+
use Illuminate\Support\Facades\Log;
1818
use Illuminate\Support\Facades\Session;
1919

2020
class SessionController extends Controller
@@ -59,13 +59,13 @@ public function login(LoginRequest $request): void
5959
}
6060

6161
if (Auth::attempt(['username' => $request->username(), 'password' => $request->password()])) {
62-
Logs::notice(__METHOD__, __LINE__, 'User (' . $request->username() . ') has logged in from ' . $request->ip());
62+
Log::channel('login')->notice(__METHOD__ . ':' . __LINE__ . ' -- User (' . $request->username() . ') has logged in from ' . $request->ip());
6363

6464
return;
6565
}
6666

6767
// TODO: We could avoid this separate log entry and let the exception handler to all the logging, if we would add "context" (see Laravel docs) to those exceptions which need it.
68-
Logs::error(__METHOD__, __LINE__, 'User (' . $request->username() . ') has tried to log in from ' . $request->ip());
68+
Log::channel('login')->error(__METHOD__ . ':' . __LINE__ . ' -- User (' . $request->username() . ') has tried to log in from ' . $request->ip());
6969

7070
throw new UnauthenticatedException('Unknown user or invalid password');
7171
}
@@ -77,6 +77,9 @@ public function login(LoginRequest $request): void
7777
*/
7878
public function logout(): void
7979
{
80+
/** @var \App\Models\User $user */
81+
$user = Auth::user();
82+
Log::channel('login')->info(__METHOD__ . ':' . __LINE__ . ' -- User (' . $user->username . ') has logged out.');
8083
Auth::logout();
8184
Session::flush();
8285
}

0 commit comments

Comments
 (0)