log FEATURE new ly_err_last() function to directly get the latest error in the context
diff --git a/src/common.h b/src/common.h
index 0e740f3..553740f 100644
--- a/src/common.h
+++ b/src/common.h
@@ -71,13 +71,6 @@
 extern volatile uint32_t ly_log_opts;
 
 /**
- * @brief Set error-app-tag to the last error record in the context.
- * @param[in] ctx libyang context where the error records are present.
- * @param[in] apptag The error-app-tag value to store.
- */
-void ly_err_last_set_apptag(const struct ly_ctx *ctx, const char *apptag);
-
-/**
  * @brief Print a log message and store it into the context (if provided).
  *
  * @param[in] ctx libyang context to store the error record. If not provided, the error is just printed.
diff --git a/src/log.c b/src/log.c
index d551095..894bffb 100644
--- a/src/log.c
+++ b/src/log.c
@@ -47,9 +47,9 @@
 {
     struct ly_err_item *i;
 
-    i = ly_err_first(ctx);
+    i = ly_err_last(ctx);
     if (i) {
-        return i->prev->no;
+        return i->no;
     }
 
     return LY_SUCCESS;
@@ -60,9 +60,9 @@
 {
     struct ly_err_item *i;
 
-    i = ly_err_first(ctx);
+    i = ly_err_last(ctx);
     if (i) {
-        return i->prev->vecode;
+        return i->vecode;
     }
 
     return LYVE_SUCCESS;
@@ -75,9 +75,9 @@
 
     LY_CHECK_ARG_RET(NULL, ctx, NULL);
 
-    i = ly_err_first(ctx);
+    i = ly_err_last(ctx);
     if (i) {
-        return i->prev->msg;
+        return i->msg;
     }
 
     return NULL;
@@ -90,9 +90,9 @@
 
     LY_CHECK_ARG_RET(NULL, ctx, NULL);
 
-    i = ly_err_first(ctx);
+    i = ly_err_last(ctx);
     if (i) {
-        return i->prev->path;
+        return i->path;
     }
 
     return NULL;
@@ -105,9 +105,9 @@
 
     LY_CHECK_ARG_RET(NULL, ctx, NULL);
 
-    i = ly_err_first(ctx);
+    i = ly_err_last(ctx);
     if (i) {
-        return i->prev->apptag;
+        return i->apptag;
     }
 
     return NULL;
@@ -142,6 +142,17 @@
     return pthread_getspecific(ctx->errlist_key);
 }
 
+API struct ly_err_item *
+ly_err_last(const struct ly_ctx *ctx)
+{
+    const struct ly_err_item *e;
+
+    LY_CHECK_ARG_RET(NULL, ctx, NULL);
+
+    e = pthread_getspecific(ctx->errlist_key);
+    return e ? e->prev : NULL;
+}
+
 API void
 ly_err_free(void *ptr)
 {
@@ -500,15 +511,3 @@
         }
     }
 }
-
-void
-ly_err_last_set_apptag(const struct ly_ctx *ctx, const char *apptag)
-{
-    struct ly_err_item *i;
-
-    i = ly_err_first(ctx);
-    if (i) {
-        i = i->prev;
-        i->apptag = strdup(apptag);
-    }
-}
diff --git a/src/log.h b/src/log.h
index 8fcb424..733f102 100644
--- a/src/log.h
+++ b/src/log.h
@@ -273,11 +273,19 @@
  * @brief Get the first (thread, context-specific) generated error structure.
  *
  * @param[in] ctx Relative context.
- * @return First error structure (can be NULL), do not modify!
+ * @return The first error structure (can be NULL), do not modify!
  */
 struct ly_err_item *ly_err_first(const struct ly_ctx *ctx);
 
 /**
+ * @brief Get the latest (thread, context-specific) generated error structure.
+ *
+ * @param[in] ctx Relative context.
+ * @return The last error structure (can be NULL), do not modify!
+ */
+struct ly_err_item *ly_err_last(const struct ly_ctx *ctx);
+
+/**
  * @brief Print the error structure as if just generated.
  *
  * @param[in] eitem Error item structure to print.