Skip to content

Commit

Permalink
Add per-application logging.
Browse files Browse the repository at this point in the history
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.

Currently these log files do not participate in log-file rotation
(SIGUSR1), that may change in a future commit. In the meantime these
logs can be rotated using the traditional copy/truncate method.

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: <#197>
Closes: <#846>
Reviewed-by: Alejandro Colomar <alx@nginx.com>
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
  • Loading branch information
ac000 committed Apr 11, 2023
1 parent 8b89529 commit 45c45ea
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/nxt_application.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 4 additions & 0 deletions src/nxt_application.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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_ */
6 changes: 6 additions & 0 deletions src/nxt_conf_validation.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/nxt_main_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ static nxt_conf_map_t nxt_common_app_conf[] = {
offsetof(nxt_common_app_conf_t, group),
},

{
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,
Expand Down
6 changes: 6 additions & 0 deletions src/nxt_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/

#include <nxt_main.h>

#include <nxt_application.h>
#include <nxt_cgroup.h>

#if (NXT_HAVE_LINUX_NS)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 45c45ea

Please sign in to comment.