summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrew Clayton <a.clayton@nginx.com>2023-03-30 05:53:13 +0100
committerAndrew Clayton <a.clayton@nginx.com>2023-04-06 19:42:22 +0100
commitdc0a2a721abe133974fa3c5a1afdc4345df640a3 (patch)
treefee5ec1fbeefe995f4df5c23a0c68e4749ee9f2f
parent5fbc363fc2d2c7463072235d35a7a83b5bdab6c0 (diff)
downloadunit-dc0a2a721abe133974fa3c5a1afdc4345df640a3.tar.gz
unit-dc0a2a721abe133974fa3c5a1afdc4345df640a3.tar.bz2
Add per-application logging.
This adds main part of per-application logging. Currently when running in the foreground, unit application processes will send stdout to the current TTY and stderr to the unit log file. That behaviour won't change. When running as a daemon, unit application processes will send stdout to /dev/null and stderr to the unit log file. This commit allows to alter the latter case of unit running as a daemon, by allowing applications to redirect stdout and/or stderr to specific log files. This is done via two new application options, 'stdout' & 'stderr', e.g "applications": { "myapp": { ... "stdout": "/path/to/log/unit/app/stdout.log", "stderr": "/path/to/log/unit/app/stderr.log" } } These log files are created by the application processes themselves and thus the log directories need to be writable by the user (and or group) of the application processes. E.g $ sudo mkdir -p /path/to/log/unit/app $ sudo chown APP_USER /path/to/log/unit/app These need to be setup before starting unit with the above config. NOTE: You may or may not see stuff printed to stdout as stdout was traditionally used by CGI applications to communicate with the webserver. Closes: <https://github.com/nginx/unit/issues/197> Closes: <https://github.com/nginx/unit/issues/846> Reviewed-by: Alejandro Colomar <alx@nginx.com> Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
-rw-r--r--src/nxt_application.c53
-rw-r--r--src/nxt_application.h4
-rw-r--r--src/nxt_conf_validation.c6
-rw-r--r--src/nxt_main_process.c12
-rw-r--r--src/nxt_process.c6
5 files changed, 81 insertions, 0 deletions
diff --git a/src/nxt_application.c b/src/nxt_application.c
index 786c768b..ffa8eb53 100644
--- a/src/nxt_application.c
+++ b/src/nxt_application.c
@@ -933,6 +933,59 @@ nxt_app_set_environment(nxt_conf_value_t *environment)
}
+nxt_int_t
+nxt_app_set_logs(void)
+{
+ nxt_int_t ret;
+ nxt_file_t file;
+ nxt_task_t *task;
+ nxt_thread_t *thr;
+ nxt_process_t *process;
+ nxt_runtime_t *rt;
+ nxt_common_app_conf_t *app_conf;
+
+ thr = nxt_thread();
+
+ task = thr->task;
+
+ rt = task->thread->runtime;
+ if (!rt->daemon) {
+ return NXT_OK;
+ }
+
+ process = rt->port_by_type[NXT_PROCESS_PROTOTYPE]->process;
+ app_conf = process->data.app;
+
+ if (app_conf->stdout_log != NULL) {
+ nxt_memzero(&file, sizeof(nxt_file_t));
+ file.log_level = 1;
+ file.name = (u_char *) app_conf->stdout_log;
+ ret = nxt_file_open(task, &file, O_WRONLY | O_APPEND, O_CREAT, 0666);
+ if (ret == NXT_ERROR) {
+ return NXT_ERROR;
+ }
+
+ nxt_file_stdout(&file);
+ nxt_file_close(task, &file);
+ }
+
+ if (app_conf->stderr_log != NULL) {
+ nxt_memzero(&file, sizeof(nxt_file_t));
+ file.log_level = 1;
+ file.name = (u_char *) app_conf->stderr_log;
+ ret = nxt_file_open(task, &file, O_WRONLY | O_APPEND, O_CREAT, 0666);
+ if (ret == NXT_ERROR) {
+ return NXT_ERROR;
+ }
+
+ nxt_file_stderr(&file);
+ nxt_file_close(task, &file);
+ }
+
+ return NXT_OK;
+}
+
+
static u_char *
nxt_cstr_dup(nxt_mp_t *mp, u_char *dst, u_char *src)
{
diff --git a/src/nxt_application.h b/src/nxt_application.h
index 4d624448..2675e6a0 100644
--- a/src/nxt_application.h
+++ b/src/nxt_application.h
@@ -92,6 +92,9 @@ struct nxt_common_app_conf_s {
nxt_str_t user;
nxt_str_t group;
+ char *stdout_log;
+ char *stderr_log;
+
char *working_directory;
nxt_conf_value_t *environment;
@@ -141,5 +144,6 @@ extern nxt_app_module_t nxt_external_module;
NXT_EXPORT nxt_int_t nxt_unit_default_init(nxt_task_t *task,
nxt_unit_init_t *init, nxt_common_app_conf_t *conf);
+NXT_EXPORT nxt_int_t nxt_app_set_logs(void);
#endif /* _NXT_APPLICATION_H_INCLIDED_ */
diff --git a/src/nxt_conf_validation.c b/src/nxt_conf_validation.c
index 9169bcc9..6d798a81 100644
--- a/src/nxt_conf_validation.c
+++ b/src/nxt_conf_validation.c
@@ -1047,6 +1047,12 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_common_members[] = {
.type = NXT_CONF_VLDT_OBJECT,
.validator = nxt_conf_vldt_isolation,
.u.members = nxt_conf_vldt_app_isolation_members,
+ }, {
+ .name = nxt_string("stdout"),
+ .type = NXT_CONF_VLDT_STRING,
+ }, {
+ .name = nxt_string("stderr"),
+ .type = NXT_CONF_VLDT_STRING,
},
NXT_CONF_VLDT_END
diff --git a/src/nxt_main_process.c b/src/nxt_main_process.c
index db1cfcb9..ae62aff4 100644
--- a/src/nxt_main_process.c
+++ b/src/nxt_main_process.c
@@ -121,6 +121,18 @@ static nxt_conf_map_t nxt_common_app_conf[] = {
},
{
+ nxt_string("stdout"),
+ NXT_CONF_MAP_CSTRZ,
+ offsetof(nxt_common_app_conf_t, stdout_log),
+ },
+
+ {
+ nxt_string("stderr"),
+ NXT_CONF_MAP_CSTRZ,
+ offsetof(nxt_common_app_conf_t, stderr_log),
+ },
+
+ {
nxt_string("working_directory"),
NXT_CONF_MAP_CSTRZ,
offsetof(nxt_common_app_conf_t, working_directory),
diff --git a/src/nxt_process.c b/src/nxt_process.c
index c7f10769..ce2de774 100644
--- a/src/nxt_process.c
+++ b/src/nxt_process.c
@@ -5,6 +5,8 @@
*/
#include <nxt_main.h>
+
+#include <nxt_application.h>
#include <nxt_cgroup.h>
#if (NXT_HAVE_LINUX_NS)
@@ -651,6 +653,10 @@ nxt_process_setup(nxt_task_t *task, nxt_process_t *process)
thread = task->thread;
rt = thread->runtime;
+ if (process->parent_port == rt->port_by_type[NXT_PROCESS_PROTOTYPE]) {
+ nxt_app_set_logs();
+ }
+
nxt_random_init(&thread->random);
rt->type = init->type;