Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing fully formatted log to syslog #172

Merged
merged 2 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 6 additions & 59 deletions prod/native/extension/code/ModuleFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,69 +58,20 @@ PHP_FUNCTION(elastic_otel_get_config_option_by_name) {
elasticApmGetConfigOption({optionName, optionNameLength}, /* out */ return_value);
}

ZEND_BEGIN_ARG_INFO_EX(elastic_otel_log_arginfo, /* _unused: */ 0, /* return_reference: */ 0, /* required_num_args: */ 7)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, isForced, IS_LONG, /* allow_null: */ 0)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, level, IS_LONG, /* allow_null: */ 0)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, category, IS_STRING, /* allow_null: */ 0)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, file, IS_STRING, /* allow_null: */ 0)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, line, IS_LONG, /* allow_null: */ 0)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, func, IS_STRING, /* allow_null: */ 0)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, message, IS_STRING, /* allow_null: */ 0)
ZEND_END_ARG_INFO()

/* {{{ elastic_otel_log(
* int $isForced,
* int $level,
* string $category,
* string $file,
* int $line,
* string $func,
* string $message
* ): void
*/
PHP_FUNCTION(elastic_otel_log) {
zend_long isForced = 0;
zend_long level = 0;
char *file = nullptr;
size_t fileLength = 0;
char *category = nullptr;
size_t categoryLength = 0;
zend_long line = 0;
char *func = nullptr;
size_t funcLength = 0;
char *message = nullptr;
size_t messageLength = 0;

ZEND_PARSE_PARAMETERS_START(/* min_num_args: */ 7, /* max_num_args: */ 7)
Z_PARAM_LONG(isForced)
Z_PARAM_LONG(level)
Z_PARAM_STRING(category, categoryLength)
Z_PARAM_STRING(file, fileLength)
Z_PARAM_LONG(line)
Z_PARAM_STRING(func, funcLength)
Z_PARAM_STRING(message, messageLength)
ZEND_PARSE_PARAMETERS_END();

ELASTICAPM_G(globals)->logger_->printf(static_cast<LogLevel>(level), "[" PRsv "] [" PRsv ":%d] [" PRsv "] " PRsv, PRcsvArg(category, categoryLength), PRcsvArg(file, fileLength), line, PRcsvArg(func, funcLength), PRcsvArg(message, messageLength));
}
/* }}} */

ZEND_BEGIN_ARG_INFO_EX(elastic_otel_logf_arginfo, /* _unused: */ 0, /* return_reference: */ 0, /* required_num_args: */ 7)
ZEND_BEGIN_ARG_INFO_EX(elastic_otel_log_feature_arginfo, /* _unused: */ 0, /* return_reference: */ 0, /* required_num_args: */ 7)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, isForced, IS_LONG, /* allow_null: */ 0)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, level, IS_LONG, /* allow_null: */ 0)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, feature, IS_LONG, /* allow_null: */ 0)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, category, IS_STRING, /* allow_null: */ 0)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, file, IS_STRING, /* allow_null: */ 0)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, line, IS_LONG, /* allow_null: */ 1)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, func, IS_STRING, /* allow_null: */ 0)
ZEND_ARG_TYPE_INFO(/* pass_by_ref: */ 0, message, IS_STRING, /* allow_null: */ 0)
ZEND_END_ARG_INFO()

/* {{{ elastic_otel_logf(
/* {{{ elastic_otel_log_feature(
* int $isForced,
* int $level,
* int $feature,
* string $category,
* string $file,
* ?int $line,
* string $func,
Expand All @@ -133,20 +84,17 @@ PHP_FUNCTION(elastic_otel_log_feature) {
zend_long feature = 0;
char *file = nullptr;
size_t fileLength = 0;
char *category = nullptr;
size_t categoryLength = 0;
zend_long line = 0;
bool lineNull = true;
char *func = nullptr;
size_t funcLength = 0;
char *message = nullptr;
size_t messageLength = 0;

ZEND_PARSE_PARAMETERS_START(/* min_num_args: */ 8, /* max_num_args: */ 8)
ZEND_PARSE_PARAMETERS_START(/* min_num_args: */ 7, /* max_num_args: */ 7)
Z_PARAM_LONG(isForced)
Z_PARAM_LONG(level)
Z_PARAM_LONG(feature)
Z_PARAM_STRING(category, categoryLength)
Z_PARAM_STRING(file, fileLength)
Z_PARAM_LONG_OR_NULL(line, lineNull)
Z_PARAM_STRING(func, funcLength)
Expand All @@ -155,10 +103,10 @@ PHP_FUNCTION(elastic_otel_log_feature) {

if (isForced || ELASTICAPM_G(globals)->logger_->doesFeatureMeetsLevelCondition(static_cast<LogLevel>(level), static_cast<elasticapm::php::LogFeature>(feature))) {
if (lineNull) {
ELASTICAPM_G(globals)->logger_->printf(static_cast<LogLevel>(level), "[" PRsv "] [" PRsv "] [" PRsv "] [" PRsv "] " PRsv, PRsvArg(elasticapm::php::getLogFeatureName(static_cast<elasticapm::php::LogFeature>(feature))), PRcsvArg(category, categoryLength), PRcsvArg(file, fileLength), PRcsvArg(func, funcLength), PRcsvArg(message, messageLength));
ELASTICAPM_G(globals)->logger_->printf(static_cast<LogLevel>(level), "[" PRsv "] [" PRsv "] [" PRsv "] " PRsv, PRsvArg(elasticapm::php::getLogFeatureName(static_cast<elasticapm::php::LogFeature>(feature))), PRcsvArg(file, fileLength), PRcsvArg(func, funcLength), PRcsvArg(message, messageLength));
return;
}
ELASTICAPM_G(globals)->logger_->printf(static_cast<LogLevel>(level), "[" PRsv "] [" PRsv "] [" PRsv ":%d] [" PRsv "] " PRsv, PRsvArg(elasticapm::php::getLogFeatureName(static_cast<elasticapm::php::LogFeature>(feature))), PRcsvArg(category, categoryLength), PRcsvArg(file, fileLength), line, PRcsvArg(func, funcLength), PRcsvArg(message, messageLength));
ELASTICAPM_G(globals)->logger_->printf(static_cast<LogLevel>(level), "[" PRsv "] [" PRsv ":%d] [" PRsv "] " PRsv, PRsvArg(elasticapm::php::getLogFeatureName(static_cast<elasticapm::php::LogFeature>(feature))), PRcsvArg(file, fileLength), line, PRcsvArg(func, funcLength), PRcsvArg(message, messageLength));
}
}
/* }}} */
Expand Down Expand Up @@ -301,8 +249,7 @@ PHP_FUNCTION(force_set_object_property_value) {
const zend_function_entry elastic_otel_functions[] = {
PHP_FE( elastic_otel_is_enabled, elastic_otel_no_paramters_arginfo )
PHP_FE( elastic_otel_get_config_option_by_name, elastic_otel_get_config_option_by_name_arginfo )
PHP_FE( elastic_otel_log, elastic_otel_log_arginfo )
PHP_FE( elastic_otel_log_feature, elastic_otel_logf_arginfo )
PHP_FE( elastic_otel_log_feature, elastic_otel_log_feature_arginfo )
PHP_FE( elastic_otel_get_last_thrown, elastic_otel_get_last_thrown_arginfo )
PHP_FE( elastic_otel_get_last_php_error, elastic_otel_get_last_php_error_arginfo )
PHP_FE( elastic_otel_hook, elastic_otel_hook_arginfo )
Expand Down
2 changes: 1 addition & 1 deletion prod/native/libcommon/code/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void LoggerSinkSysLog::setLevel(LogLevel level) {

void LoggerSinkSysLog::writeLog(std::string const &formattedOutput, std::string_view message, std::string_view time, std::string_view level, std::string_view process) const {
// mt-safe (multithread)
::syslog(LOG_ALERT, PRsv " " PRsv, PRsvArg(process), PRsvArg(message));
::syslog(LOG_ALERT, PRsv " " PRsv, PRsvArg(formattedOutput), PRsvArg(formattedOutput));
}

LogLevel LoggerSinkFile::getLevel() const {
Expand Down
3 changes: 0 additions & 3 deletions prod/php/ElasticOTel/BootstrapStageLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
*/
final class BootstrapStageLogger
{
public const LOG_CATEGORY = 'Bootstrap';

public const LEVEL_OFF = 0;
public const LEVEL_CRITICAL = 1;
public const LEVEL_ERROR = 2;
Expand Down Expand Up @@ -242,7 +240,6 @@ private static function logWithLevel(int $statementLevel, string $message, strin
0 /* $isForced */,
$statementLevel,
Log\LogFeature::BOOTSTRAP,
self::LOG_CATEGORY,
self::processSourceCodeFilePathForLog($file),
$line,
self::processClassFunctionNameForLog($class, $func),
Expand Down
16 changes: 8 additions & 8 deletions prod/php/ElasticOTel/InstrumentationBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* under the License.
*/

/** @noinspection PhpIllegalPsrClassPathInspection */

declare(strict_types=1);

namespace Elastic\OTel;
Expand Down Expand Up @@ -178,24 +180,22 @@ private static function placeDebugHooks(?string $class, string $function): void
$function,
function () use ($func) {
elastic_otel_log_feature(
0,
0 /* <- isForced */,
LogLevel::debug->value,
Log\LogFeature::INSTRUMENTATION,
'PRE HOOK',
'',
null,
'' /* <- file */,
null /* <- line */,
$func,
('pre-hook data: ' . var_export(func_get_args(), true))
);
},
function () use ($func) {
elastic_otel_log_feature(
0,
0 /* <- isForced */,
LogLevel::debug->value,
Log\LogFeature::INSTRUMENTATION,
'POST HOOK',
'',
null,
'' /* <- file */,
null /* <- line */,
$func,
('post-hook data: ' . var_export(func_get_args(), true))
);
Expand Down
1 change: 0 additions & 1 deletion prod/php/ElasticOTel/Log/ElasticLogWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public function write(mixed $level, string $message, array $context): void
0 /* <- isForced */,
$edotLevel->value,
LogFeature::OTEL,
'OpenTelemetry' /* <- category */,
$caller['file'] ?? '',
$caller['line'] ?? null,
$func,
Expand Down
1 change: 0 additions & 1 deletion tests/elastic_otel_extension_stubs/global_namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ function elastic_otel_log_feature(
int $isForced,
int $level,
int $feature,
string $category,
string $file,
?int $line,
string $func,
Expand Down
Loading