Answer the question
In order to leave comments, you need to log in
How to organize the code in this case?
Hello! I need to cover the project with logs. In some parts of the code, you need to write data to the database. I have identified several entities that I need to log:
1. User request
2. Server response to the user
3. Exceptions
4. Payment system responses
Probably something else will need to be logged later, but so far so.
I decided to do this:
interface LoggerInterface
{
public function log($data);
}
class RequestLogger implements LoggerInterface
{
public function log($request)
{
$activeRecord->insert(
'request_id' => $request->id,
'headers' => $request->headers,
//...
);
}
}
class ResponseLogger implements LoggerInterface
{
public function log($request, $response)
{
$activeRecord->insert(
'request_id' => $request->id,
'headers' => $response->headers,
//...
);
}
}
class ExceptionLogger implements LoggerInterface
{
public function log($exception)
{
$activeRecord->insert(
'message' => $exception->getLine() . ' ' . $exception->getMessage(),
//...
);
}
}
class CloudPaymentsLogger implements LoggerInterface
{
public function log($data)
{
$activeRecord->insert(
'status' => $data->status,
'data' => $data->message,
//...
);
}
}
Answer the question
In order to leave comments, you need to log in
In general, if you need to screw up logging, just install the package https://github.com/php-fig/log
and don't be distracted by reinventing the wheel. Do some really useful work - covering logs.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question