signalizejs/logger

Wrapper around console (log, info, warning, error) for sending JS log info to the server.

Installation

Required import map dependencies:
import loggerModule from 'signalizejs/logger';

const signalize = new Signalize({
	modules: [
		[
			'logger',
			loggerModule,
			{
				// URL where the logs will be sent
				url: 'https://example.com/api/log-client-error',
				// Optional
				// What type of info should be sent
				// Default is "error" only
				levels: ['log', 'warning', 'error']
			}
		]
	]
})

Usage

The logger plugin starts automatically and doesn’t have any API:

  • After the module setup, the logger will automatically start sending requests to the configured endpoint.
  • The default level that is sent is error only.
  • You can change it by passing levels during initialization.

Handling errors on the server side

The logger sends a JSON with the following keys:

  • message: Error message text.
  • file: File path.
  • lineNumber: Line number where the error occured.
  • columnNumber: Column numberwhere the error occured.
  • stack: Call stack

Simplified example with the Symfony framework in PHP

#[Route('/log-client-error')]
public function clientError(Request $request)
{
	$logsDir = $this->kernel->getLogDir();
	$counterFile = $logsDir . '/js-errors-counter.txt';
	$logsFile = $logsDir . '/js-errors.log';
	$counter = 0;

	if (is_file($counterFile)) {
		$counter = (int) file_get_contents($counterFile);
	}

	// Set max amount of logs to 50
	if ($counter === 50) {
		return new Response('');
	}

	$counter ++;
	file_put_contents($counterFile, $counter);

	// Get the request content and the log data
	// After that, you can work with the keys (like "message", "file") within the log mentioned above
	$log = json_decode($request->getContent(), false)->log;
	// Get the current date and time
	$currentTime = date('Y-m-d H:i:s');

	// Create a DateTime object from the current time
	$dateTime = new DateTime($currentTime);

	// Format the date and time using desired format specifiers
	$formattedTime = $dateTime->format('h:i j. n. Y');

	file_put_contents($logsFile, '[' . $formattedTime . '] (' . $log->url . '): ' . preg_replace('/\n/', ' ', $log->message) . PHP_EOL, FILE_APPEND);

	return new Response('');
}