CHANGE use syslog if daemon
diff --git a/src/netopeerguid.c b/src/netopeerguid.c
index 90f19b7..3059c78 100644
--- a/src/netopeerguid.c
+++ b/src/netopeerguid.c
@@ -56,6 +56,7 @@
 #include <sys/un.h>
 #include <sys/stat.h>
 #include <pwd.h>
+#include <syslog.h>
 #include <errno.h>
 #include <limits.h>
 #include <grp.h>
@@ -113,6 +114,7 @@
 pthread_key_t err_reply_key;
 volatile int isterminated = 0;
 static char* password;
+int daemonize;
 
 json_object *create_ok_reply(void);
 json_object *create_data_reply(const char *data);
@@ -2086,16 +2088,16 @@
 {
     switch (level) {
     case NC_VERB_ERROR:
-        ERROR("ERROR: %s", msg);
+        ERROR("lib ERROR: %s", msg);
         break;
     case NC_VERB_WARNING:
-        ERROR("WARNING: %s", msg);
+        ERROR("lib WARNING: %s", msg);
         break;
     case NC_VERB_VERBOSE:
-        ERROR("VERBOSE: %s", msg);
+        ERROR("lib VERBOSE: %s", msg);
         break;
     case NC_VERB_DEBUG:
-        DEBUG("DEBUG: %s", msg);
+        DEBUG("lib DEBUG: %s", msg);
         break;
     }
 
@@ -3837,7 +3839,7 @@
 {
     struct sigaction action;
     sigset_t block_mask;
-    int daemonize = 0, i;
+    int i;
 
     if (argc > 3) {
         printf("Usage: [--(h)elp] [--(d)aemon] [socket-path]\n");
@@ -3856,9 +3858,12 @@
         }
     }
 
-    if (daemonize && (daemon(0, 0) == -1)) {
-        ERROR("daemon() failed (%s)", strerror(errno));
-        return 1;
+    if (daemonize) {
+        if (daemon(0, 0) == -1) {
+            ERROR("daemon() failed (%s)", strerror(errno));
+            return 1;
+        }
+        openlog("netopeerguid", LOG_PID, LOG_DAEMON);
     }
 
     sigfillset(&block_mask);
@@ -3870,5 +3875,8 @@
 
     forked_proc();
     DEBUG("Terminated");
+    if (daemonize) {
+        closelog();
+    }
     return 0;
 }
diff --git a/src/netopeerguid.h b/src/netopeerguid.h
index ebb8d5f..a087efb 100644
--- a/src/netopeerguid.h
+++ b/src/netopeerguid.h
@@ -51,6 +51,7 @@
 
 #include <pthread.h>
 #include <json.h>
+#include <syslog.h>
 #include <libyang/libyang.h>
 
 #define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
@@ -89,18 +90,25 @@
 
 extern pthread_key_t err_reply_key;
 extern pthread_mutex_t json_lock;
+extern int daemonize;
 
 json_object *create_error_reply(const char *errmess);
 
-#define DEBUG(...) do { \
+#define DEBUG(...) \
+if (daemonize) { \
+    syslog(LOG_DEBUG, __VA_ARGS__); \
+} else { \
     fprintf(stderr, __VA_ARGS__); \
     fprintf(stderr, "\n"); \
-} while (0);
+}
 
-#define ERROR(...) do { \
+#define ERROR(...) \
+if (daemonize) { \
+    syslog(LOG_ERR, __VA_ARGS__); \
+} else { \
     fprintf(stderr, __VA_ARGS__); \
     fprintf(stderr, "\n"); \
-} while (0);
+}
 
 #define GETSPEC_ERR_REPLY \
 json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key); \