I want to log each node execution in physical file using javascript

Hello Guys currently i have created a workflow and i want to log each node execution so i have created a code node and connected all nodes to code node & i am using javascript inside code but i am unable to get node names while logging

Describe the problem/error/question

What is the error message (if any)?

Please share your workflow


const logTime = new Date().toISOString();
let log = `[${logTime}] Workflow Execution Logs:\n\n`;



const inputs = $input.all();
const execKeys = Object.keys($execution || {});

inputs.forEach((entry, index) => {
 let sourceNode = entry.json?.nodeNames || entry.source?.node || entry.source?.nodeNames || `Input #${index + 1}`;


  let bestMatch = null;
  let highestScore = 0;

  if (!sourceNode || /^Input\s?#?\d*$/i.test(sourceNode)) {
    for (const key of execKeys) {
      const execEntry = $execution[key]?.[0];
      if (!execEntry?.data) continue;

      const execData = execEntry.data;
      let score = 0;

      for (const k of Object.keys(entry.json)) {
        if (execData[k] && JSON.stringify(execData[k]) === JSON.stringify(entry.json[k])) {
          score++;
        }
      }

      if (score > highestScore) {
        highestScore = score;
        bestMatch = key;
      }
    }

    if (bestMatch && highestScore > 0) {
      sourceNode = bestMatch;
    } else if (execKeys.length === 1) {
      sourceNode = execKeys[0];
    } else {
      sourceNode = `Unknown Node (${index + 1})`;
    }
  }

  const execInfo = $execution?.[sourceNode]?.[0];

  if (execInfo?.error) {
    log += `    Status: ❌ Error\n`;
    log += `    Error: ${execInfo.error.message || JSON.stringify(execInfo.error)}\n`;
  } else {
    log += `    Status: ✅ Success\n`;
  }

  const data = entry.json;
  if (Object.keys(data).length === 0) {
    log += `    Output: (no data)\n`;
  } else {
    log += `    Output:\n`;
    for (const key of Object.keys(data)) {
      log += `      - ${key}: ${JSON.stringify(data[key])}\n`;
    }
  }

  log += '\n';
});

return [{ json: { log } }];

Share the output returned by the last node

[
{
“log”:
“[2025-12-04T14:56:20.764Z] Workflow Execution Logs:\n\n Status: :white_check_mark: Success\n Output:\n - event: “Manual execution”\n - timestamp: “2025-12-04T14:56:19.746Z”\n - workflow_id: “jviZsgdWHvsiwlVe”\n\n”
}
]

Information on your n8n setup

  • n8n version:1.90
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @AravindParitala , welcome!

I don’t think nodeNames is valid..
Also, from the screenshot, I’m not sure how you’re getting the execution details..
Are you getting them through the HTTP request after the trigger?

BTW, since you’re already using the n8n API, why not simply use Get Execution Details and extract what you need from there?

For example:

Hello @mohamed3nan i am sending all nodes output to log node and previously i was able to save that but the issue i have is i am unable to capture node names. i want to save the logs each node wise in an order so. Without node names i am able to store each node output

How were you able to get the names previously?

I mean from your screenshot, is this HTTP request calling an endpoint that gives you the execution details?

It would be easier to help if you included some pinned data with the workflow.

@mohamed3nan
I am not able to get node names for that what i did in edit fields node i have manually added names in sequential order. Why because i am not able to find any way to read node names from prepare log node so.

Http Request in screenshot will give us the required service details. Bascially we are trying to fetch wether the service is healthy or not healthy.
Here is the response we are getting from that http request

[

{

“cluster_name”: “docker-cluster”,

“status”: “green”,

“timed_out”: false,

“number_of_nodes”: 1,

“number_of_data_nodes”: 1,

“active_primary_shards”: 32,

“active_shards”: 32,

“relocating_shards”: 0,

“initializing_shards”: 0,

“unassigned_shards”: 0,

“delayed_unassigned_shards”: 0,

“number_of_pending_tasks”: 0,

“number_of_in_flight_fetch”: 0,

“task_max_waiting_in_queue_millis”: 0,

“active_shards_percent_as_number”: 100

}

]

I’m still not sure I follow, So you’re manually entering the names??

If you can attach the workflow, it will be easier for others to look at the logic and help.

AFAIK to get a previous node’s name, you can use:{{ $prevNode.name }}

If you need a more advanced setup (for example, to get all node names), you’ll need to use the n8n API to get the workflow JSON and parse it, check this workflow logic where I extracted node names:

The “Prepare Log” node isn’t connected, so you can’t reference it.

@mohamed3nan yes i am adding names manually
THanks for the rsponse i will try to do the way you did.
I have one more important issue can you please help me on it, i will tag you for that question

@mohamed3nan can you please help me how can we pass a node name to another node