TECNOLOBO

No recuerdas tu codigo?
Se te olvido como se hace?

Aqui podras guardar lo que necesiten
Y cuando sea necesesario

Creado por julian gomez
iiiiii

Guardar y consultar losg



Descripcion

Para guardar los errores en tablas en N8N

Notas


# 1 Es necesario una tabla en base de datos PosrtgresSql
# 2 Se necestia un Subworkflow para gurdar los errores en la tabla postgres y poder usarlo en multiples partes
# 3 Es Necesario llamar el subworkflow en cada uno de los nodos que se desean hacer seguimiento
# 4 cada nodo debe tener configurado en su setting "ERROR" = continue con error log.


#####################
#############
 para saber que flujo fue el que disparo el error se debe hacerlo mediante la URL de la siguiente forma
#############
##############################################################################################
https://n8n.tecnolobo.com/workflow/16Uap78QdvnnR5Jv/executions/397?projectId=6CHcM6AJy1wqWP83&parentFolderId=YgGm4ZdhgBnz2epi&uiContext=workflow_list

397 = id de la ejecucion del workflow se cambia por la que se necesite.
16Uap78QdvnnR5Jv = es el ID del flujo del trabajo.




#####################################################################################################
############ La siguiente estructura es la base de datos en postgresql ##############################
#####################################################################################################
CREATE TABLE workflow_logs (
  id             SERIAL PRIMARY KEY,
  level          VARCHAR(10)  NOT NULL CHECK (level IN ('error', 'warn', 'info', 'success')),
  workflow_name  VARCHAR(255) NOT NULL,
  workflow_id    VARCHAR(100),
  execution_id   VARCHAR(100),
  execution_mode VARCHAR(50),
  node_name      VARCHAR(255),
  message        TEXT         NOT NULL,
  context_data   JSONB,
  error_stack    TEXT,
  created_at     TIMESTAMPTZ  NOT NULL DEFAULT NOW()
);

-- ?ndices para queries frecuentes
CREATE INDEX idx_wl_level        ON workflow_logs (level);
CREATE INDEX idx_wl_workflow_name ON workflow_logs (workflow_name);
CREATE INDEX idx_wl_created_at   ON workflow_logs (created_at DESC);
CREATE INDEX idx_wl_context      ON workflow_logs USING GIN (context_data);




						

Workflow-json


{
  "name": "flujo de prueba para logs",
  "nodes": [
    {
      "parameters": {
        "url": "http://tecnolobos.com",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        -48,
        -32
      ],
      "id": "6a9f02e9-691a-42fd-bf1b-46d67448bcd2",
      "name": "HTTP Request",
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "workflowId": {
          "__rl": true,
          "value": "R456ENGDYyBZA47i",
          "mode": "list",
          "cachedResultUrl": "/workflow/R456ENGDYyBZA47i",
          "cachedResultName": "Guardar_logs"
        },
        "workflowInputs": {
          "mappingMode": "defineBelow",
          "value": {
            "level": "error",
            "workflow_name": "={{ $workflow.name }}",
            "workflow_id": "={{ $workflow.id }}",
            "execution_id": "={{ $execution.id }}",
            "execution_mode": "={{ $execution.mode }}",
            "node_name": "={{ $prevNode.name }}",
            "message": "={{ $json.error.message }}",
            "error_stack": "={{ $json.error.stack }}",
            "context_data": "={\n\"simulaFallo\":true\n}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "level",
              "displayName": "level",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "workflow_name",
              "displayName": "workflow_name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "workflow_id",
              "displayName": "workflow_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "execution_id",
              "displayName": "execution_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "execution_mode",
              "displayName": "execution_mode",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "node_name",
              "displayName": "node_name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "message",
              "displayName": "message",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "context_data",
              "displayName": "context_data",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "error_stack",
              "displayName": "error_stack",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        },
        "options": {}
      },
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.3,
      "position": [
        224,
        112
      ],
      "id": "9215f42e-41df-437b-874f-4d7a3d93da36",
      "name": "Grabar Log",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -288,
        -32
      ],
      "id": "e4fe31f4-e5ac-475c-9ad9-4a7792c5bb46",
      "name": "Inicio"
    }
  ],
  "pinData": {},
  "connections": {
    "HTTP Request": {
      "main": [
        [],
        [
          {
            "node": "Grabar Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Inicio": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "9b321105-5003-4c7f-b4be-c48f0ead7660",
  "meta": {
    "instanceId": "fe291777ffabf9d737d29c561fd9bdf11a3d8ba106af6fb4a8b650410061a9ea"
  },
  "nodeGroups": [],
  "id": "16Uap78QdvnnR5Jv",
  "tags": []
}
						

SubWorkflow-json


{
  "name": "Guardar_logs",
  "nodes": [
    {
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "level"
            },
            {
              "name": "workflow_name"
            },
            {
              "name": "workflow_id"
            },
            {
              "name": "execution_id"
            },
            {
              "name": "execution_mode"
            },
            {
              "name": "node_name"
            },
            {
              "name": "message"
            },
            {
              "name": "context_data"
            },
            {
              "name": "error_stack"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.2,
      "position": [
        -480,
        -96
      ],
      "id": "a9cf8e81-51ba-480e-ac21-0d8ababe1000",
      "name": "When Executed by Another Workflow"
    },
    {
      "parameters": {
        "jsCode": "const input =  $input.first().json;\nlet level = null;\nlet workflow_name = input.workflow_name;\nlet message = input.message;\n\nconst validLevels = {\n  error: 'error',\n  warn: 'warn', \n  info: 'info', \n  success: 'success'\n};\n\nconsole.log('workloif_',input.workflow_id);\n\nif(input.workflow_id){\n  level = validLevels[input.level?.toLowerCase()] || 'info';\n\n  if(!message){\n    message = 'Sin mensaje';\n  }\n\n  if(!workflow_name){\n    workflow_name = 'unknown';\n  }\n    \n  \n}else{\n  level = null;\n  workflow_name = null;\n  \n}\n\n\n\nreturn {\n  level: level,\n    workflow_name:  workflow_name,\n    workflow_id:    input.workflow_id    ?? null,\n    execution_id:   input.execution_id  ?? null,\n    execution_mode: input.execution_mode ?? null,\n    node_name:      input.node_name      ?? null,\n    message:        message                     ,\n    context_data:   input.context_data   ? input.context_data : null,\n    error_stack:    input.error_stack    ?? null,\n}"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -224,
        -96
      ],
      "id": "fa59112f-fa68-4727-aebf-b21dcd7fd3e4",
      "name": "Validate columns"
    },
    {
      "parameters": {
        "schema": {
          "__rl": true,
          "value": "public",
          "mode": "list",
          "cachedResultName": "public"
        },
        "table": {
          "__rl": true,
          "value": "workflow_logs",
          "mode": "list",
          "cachedResultName": "workflow_logs"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "level": "={{ $json.level }}",
            "workflow_name": "={{ $json.workflow_name }}",
            "workflow_id": "={{ $json.workflow_id }}",
            "execution_id": "={{ $json.execution_id }}",
            "execution_mode": "={{ $json.execution_mode }}",
            "node_name": "={{ $json.node_name }}",
            "message": "={{ $json.message }}",
            "context_data": "={{ $json.context_data }}",
            "error_stack": "={{ $json.error_stack }}"
          },
          "matchingColumns": [
            "id"
          ],
          "schema": [
            {
              "id": "id",
              "displayName": "id",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "level",
              "displayName": "level",
              "required": true,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "workflow_name",
              "displayName": "workflow_name",
              "required": true,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "workflow_id",
              "displayName": "workflow_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "execution_id",
              "displayName": "execution_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "execution_mode",
              "displayName": "execution_mode",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "node_name",
              "displayName": "node_name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "message",
              "displayName": "message",
              "required": true,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "context_data",
              "displayName": "context_data",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "object",
              "canBeUsedToMatch": true
            },
            {
              "id": "error_stack",
              "displayName": "error_stack",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "created_at",
              "displayName": "created_at",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": true,
              "removed": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        224,
        -192
      ],
      "id": "41279419-e05b-4a1f-bd6e-6bc23b8ddab0",
      "name": "Insert rows in a table",
      "credentials": {
        "postgres": {
          "id": "7GEn0NIPS83s2Cr8",
          "name": "Postgres n8n"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "fa32495e-ada4-405c-8468-461be757cb1a",
              "leftValue": "={{ $json.workflow_id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        16,
        -96
      ],
      "id": "0075ec27-0305-43f6-a0ba-c3d2633ce288",
      "name": "If"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        208,
        16
      ],
      "id": "22d2239e-ba03-4461-87b3-3c219cd2b266",
      "name": "No Operation, do nothing"
    }
  ],
  "pinData": {},
  "connections": {
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Validate columns",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate columns": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Insert rows in a table",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "47b93fd7-62fd-4179-a9f1-b01a857e4727",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "fe291777ffabf9d737d29c561fd9bdf11a3d8ba106af6fb4a8b650410061a9ea"
  },
  "nodeGroups": [],
  "id": "R456ENGDYyBZA47i",
  "tags": []
}