Skip to content

Commit

Permalink
add stdout json logging
Browse files Browse the repository at this point in the history
  • Loading branch information
winmillwill authored and djschaap committed May 22, 2024
1 parent 94a4ace commit 2e86294
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/fusedav_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static int fusedav_opt_proc(void *data, const char *arg, int key, struct fuse_ar
break;

case KEY_IGNORE:
return 0;
return 0;

case KEY_HELP:
fprintf(stderr,
Expand Down Expand Up @@ -130,6 +130,7 @@ static void print_config(struct fusedav_config *config) {
log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "log_level %d", config->log_level);
log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "log_level_by_section %s", config->log_level_by_section);
log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "log_prefix %s", config->log_prefix);
log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "log_destination %s", config->log_destination);
log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "max_file_size %d", config->max_file_size);
log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "statsd_host %s", config->statsd_host);
log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "statsd_port %s", config->statsd_port);
Expand All @@ -156,6 +157,7 @@ run_as_gid=6f7a106722f74cc7bd96d4d06785ed78
log_level=5
log_level_by_section=0
log_prefix=6f7a106722f74cc7bd96d4d06785ed78
log_destination=journal
max_file_size=256
statsd_host=127.0.0.1
statsd_port=8126
Expand Down Expand Up @@ -198,6 +200,7 @@ static void parse_configs(struct fusedav_config *config, GError **gerr) {
keytuple(fusedav, log_level, INT),
keytuple(fusedav, log_level_by_section, STRING),
keytuple(fusedav, log_prefix, STRING),
keytuple(fusedav, log_destination, STRING),
keytuple(fusedav, max_file_size, INT),
keytuple(fusedav, statsd_host, STRING),
keytuple(fusedav, statsd_port, STRING),
Expand Down Expand Up @@ -290,6 +293,7 @@ void configure_fusedav(struct fusedav_config *config, struct fuse_args *args, ch
config->nodaemon = false;
config->max_file_size = 256; // 256M
config->log_level = 5; // default log_level: LOG_NOTICE
config->log_destination = JOURNAL;
asprintf(&config->statsd_host, "%s", "127.0.0.1");
asprintf(&config->statsd_port, "%s", "8126");

Expand All @@ -312,7 +316,7 @@ void configure_fusedav(struct fusedav_config *config, struct fuse_args *args, ch

asprintf(&user_agent, "FuseDAV/%s %s", PACKAGE_VERSION, config->log_prefix);

log_init(config->log_level, config->log_level_by_section, config->log_prefix);
log_init(config->log_level, config->log_level_by_section, config->log_prefix, config->log_destination);
log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "log_level: %d.", config->log_level);

if (stats_init(config->statsd_host, config->statsd_port) < 0) {
Expand Down
1 change: 1 addition & 0 deletions src/fusedav_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct fusedav_config {
int log_level;
char *log_level_by_section;
char *log_prefix;
char *log_destination;
int max_file_size;
char *statsd_host;
char *statsd_port;
Expand Down
40 changes: 33 additions & 7 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <syscall.h>
#include <assert.h>
#include <stdlib.h>
#include <glib.h>

#include "log.h"
#include "log_sections.h"
Expand Down Expand Up @@ -53,10 +54,19 @@ __thread unsigned int LOG_DYNAMIC = LOG_INFO;
// max size for strings in log_key_value array
#define KVITEM_SIZE 64

static const char *log_template = "{\"MESSAGE\": \"%s%s\", "
"\"PRIORITY\": %d, "
"\"USER_AGENT\": \"%s\", "
"\"SITE\": \"%s\", "
"\"ENVIRONMENT\": \"%s\", "
"\"HOST_ADDRESS\": \"%s\", "
"\"TID\": \"%lu\", "
"\"PACKAGE_VERSION\": \"%s\"}\n";

static unsigned int global_log_level = 5;
static unsigned int section_log_levels[SECTIONS] = {0};
static const char *log_key_value[KVITEMS];

static enum log_destination log_destination = JOURNAL;
static const char *errlevel[] = {"EMERG: ", "ALERT: ", "CRIT: ", "ERR: ", "WARN: ", "NOTICE: ", "INFO: ", "DEBUG: "};

// From the base url get the site id and site env
Expand Down Expand Up @@ -85,7 +95,7 @@ static void initialize_site(void) {
}

/* The log_prefix comes from fusedav.conf; the base_url from curl and fuse. */
void log_init(unsigned int log_level, const char *log_level_by_section, const char *user_agent_abbrev) {
void log_init(unsigned int log_level, const char *log_level_by_section, const char *user_agent_abbrev, const char *destination) {

unsigned int vlen;

Expand All @@ -100,6 +110,10 @@ void log_init(unsigned int log_level, const char *log_level_by_section, const ch
log_key_value[USER_AGENT_ABBREV] = "(null)";
}

if ((destination != NULL) && strncmp(destination, "stdout", sizeof("stdout")) == 0) {
log_destination = STDOUT;
}

initialize_site();

if (log_level_by_section == NULL) return;
Expand Down Expand Up @@ -168,9 +182,22 @@ int logging(unsigned int log_level, unsigned int section) {
}

static int print_it(const char const *formatwithtid, const char const *msg, int log_level) {
int ret;
// fusedav-server standardizing on names BINDING, SITE, and ENVIRONMENT
ret = sd_journal_send("MESSAGE=%s%s", formatwithtid, msg,
if (log_destination == STDOUT) {
char * escaped = g_strescape(msg, NULL);
printf(log_template,
formatwithtid, escaped,
log_level,
get_user_agent(),
log_key_value[SITE],
log_key_value[ENVIRONMENT],
log_key_value[HOST_ADDRESS],
syscall(SYS_gettid),
PACKAGE_VERSION,
NULL);
free(escaped);
return 0;
}
return sd_journal_send("MESSAGE=%s%s", formatwithtid, msg,
"PRIORITY=%d", log_level,
"USER_AGENT=%s", get_user_agent(),
"SITE=%s", log_key_value[SITE],
Expand All @@ -179,7 +206,6 @@ static int print_it(const char const *formatwithtid, const char const *msg, int
"TID=%lu", syscall(SYS_gettid),
"PACKAGE_VERSION=%s", PACKAGE_VERSION,
NULL);
return ret;
}

#define max_msg_sz 2048
Expand All @@ -196,7 +222,7 @@ int log_print(unsigned int log_level, unsigned int section, const char *format,
assert(formatwithlevel);

// print the intended message
ret = print_it(formatwithlevel, msg, log_level);
print_it(formatwithlevel, msg, log_level);

// Check and see if we're no longer doing dynamic logging. If so, it will take effect after this call. Then print a message
if (turning_off_dynamic_logging()) {
Expand Down
8 changes: 7 additions & 1 deletion src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@

extern __thread unsigned int LOG_DYNAMIC;

void log_init(unsigned int log_level, const char *log_level_by_section, const char *user_agent);
enum log_destination {
JOURNAL,
STDOUT
};


void log_init(unsigned int log_level, const char *log_level_by_section, const char *user_agent, const char *destination);
int log_print(unsigned int log_level, unsigned int section, const char *format, ...);
int logging(unsigned int log_level, unsigned int section);
void set_dynamic_logging(void);

0 comments on commit 2e86294

Please sign in to comment.