validation OPTIMIZE set log nodes only before an error

Some logging improvements included.
diff --git a/src/log.c b/src/log.c
index a6b1fe4..74407d8 100644
--- a/src/log.c
+++ b/src/log.c
@@ -618,8 +618,8 @@
 
     if (log_location.paths.count && ((const char *)(log_location.paths.objs[log_location.paths.count - 1]))[0]) {
         /* simply get what is in the provided path string */
-        *path = strdup((const char *)log_location.paths.objs[log_location.paths.count - 1]);
-        LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM);
+        r = asprintf(path, "Path \"%s\".", (const char *)log_location.paths.objs[log_location.paths.count - 1]);
+        LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
     } else {
         /* data/schema node */
         if (log_location.dnodes.count) {
@@ -700,12 +700,12 @@
  * @param[in] plugin_name Name of the plugin generating the message.
  * @param[in] level Log message level (error, warning, etc.)
  * @param[in] err_no Error type code.
- * @param[in] path Optional path of the error.
+ * @param[in] path Optional path of the error, used if set.
  * @param[in] format Format string to print.
  * @param[in] ap Var arg list for @p format.
  */
 static void
-ly_ext_log(const struct ly_ctx *ctx, const char *plugin_name, LY_LOG_LEVEL level, LY_ERR err_no, const char *path,
+ly_ext_log(const struct ly_ctx *ctx, const char *plugin_name, LY_LOG_LEVEL level, LY_ERR err_no, char *path,
         const char *format, va_list ap)
 {
     char *plugin_msg;
@@ -718,8 +718,7 @@
         return;
     }
 
-    log_vprintf(ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err_no, LYVE_OTHER, path ? strdup(path) : NULL, NULL,
-            plugin_msg, ap);
+    log_vprintf(ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err_no, LYVE_OTHER, path, NULL, plugin_msg, ap);
     free(plugin_msg);
 }
 
@@ -737,8 +736,6 @@
     va_start(ap, format);
     ly_ext_log(PARSER_CTX(pctx), ext->record->plugin.id, level, err_no, path, format, ap);
     va_end(ap);
-
-    free(path);
 }
 
 LIBYANG_API_DEF void
@@ -746,9 +743,15 @@
         const char *format, ...)
 {
     va_list ap;
+    char *path = NULL;
+
+    if (cctx && (asprintf(&path, "Path \"%s\".", cctx->path) == -1)) {
+        LOGMEM(cctx->ctx);
+        return;
+    }
 
     va_start(ap, format);
-    ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, cctx ? cctx->path : NULL, format, ap);
+    ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, path, format, ap);
     va_end(ap);
 }
 
@@ -757,13 +760,38 @@
         const char *format, ...)
 {
     va_list ap;
+    char *log_path = NULL;
+
+    if (path && (asprintf(&log_path, "Path \"%s\".", path) == -1)) {
+        LOGMEM(ext->module->ctx);
+        return;
+    }
 
     va_start(ap, format);
-    ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, path, format, ap);
+    ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, log_path, format, ap);
     va_end(ap);
 }
 
 /**
+ * @brief Serves only for creating ap.
+ */
+static void
+_lyplg_ext_compile_log_err(const struct ly_err_item *err, const struct lysc_ext_instance *ext, ...)
+{
+    va_list ap;
+
+    va_start(ap, ext);
+    ly_ext_log(ext->module->ctx, ext->def->plugin->id, err->level, err->no, err->path ? strdup(err->path) : NULL, "%s", ap);
+    va_end(ap);
+}
+
+LIBYANG_API_DEF void
+lyplg_ext_compile_log_err(const struct ly_err_item *err, const struct lysc_ext_instance *ext)
+{
+    _lyplg_ext_compile_log_err(err, ext, err->msg);
+}
+
+/**
  * @brief Exact same functionality as ::ly_err_print() but has variable arguments so log_vprintf() can be called.
  */
 static void