View logs of firebase functions in real time

17,394

Solution 1

You can see logs in real time using the Firebase console. Choose your project, click the Functions product on the left, then click the Logs tab.

Solution 2

You can also see logs of deployed functions in console/cmd:

Using the Firebase CLI
To view logs with the firebase tool, use the functions:log command:

firebase functions:log

To view logs for a specific function, provide the function name as an argument:

firebase functions:log --only <FUNCTION_NAME>

For the full range of log viewing options, view the help for functions:log:

firebase help functions:log

https://firebase.google.com/docs/functions/writing-and-viewing-logs#viewing_logs

Solution 3

For those of us that prefer a CLI version

npx firebase-logging --project=YOUR_PROJECT --freq=1500

This will fetch logs from Firebase, refreshed every 1.5s. Thanks for Geoffery and his answer, for inspiration.

Solution 4

The logs are viewable in the Firebase console (Functions -> Logs), but they are very hard to review with details hidden on accordion expansions.

Using the CLI you can specify the numbers of lines to return with the "-n" parameter:

firebase functions:log -n 100

You can also simulate real time logs by continually getting the logs and printing out the new entries.

https://github.com/fireRun-io/firebase-logging

const cmd = require("node-cmd");
const argv = require("yargs").argv;

let last = [];
const project = argv.project ? `--project ${argv.project}` : "";
const lines = argv.lines ? argv.lines : 100;

if (argv.h) {
  console.log(
    "Format: node firebase-logging.js --project=[projectId] --n=[number of lines]"
  );
  return;
}

const getLogs = () => {
  cmd.get(
    `firebase ${project} functions:log -n ${lines}`,
    (err, data, stderr) => {
      if (err) {
        console.error(err);
      }

      const splitData = data.trim().split("\n");
      const diff = splitData.filter((x) => !last.includes(x));

      if (diff.length > 0) {
        console.log(diff.join("\n"));
      }

      last = [...splitData];
    }
  );
};

getLogs();
setInterval(() => {
  getLogs();
}, 2000);

Share:
17,394
Sergio76
Author by

Sergio76

Updated on June 21, 2022

Comments

  • Sergio76
    Sergio76 almost 2 years

    I'm using the firebase functions and it's the first time I use node.

    My question is very simple, or so I think.

    I create a simple function, where, theoretically, if a new field is added to the database, this function must react.

    my code is the following:

    const functions = require('firebase-functions');
    
    
    'use strict';
    
    
    exports.newItem = functions.database.ref('/test')
      .onCreate((snapshot, context) => {
    
    
        snapshot = change.after;
        const val = snapshot.val();
    
        console.log('i´m alive');
    
        return null;
      })
    
    ;
    

    I'm looking for where to see if the log that I have inside the "newItem" function is shown or not.

    As I read, I use "firebase functions: log" from the console, but this returns previous logs, not real time.

    I have also seen the use of: gcloud functions logs read, as I read here.

    But this always returns: -bash: gcloud: command not found I have installed "gcloud functions", but I'm still lost.

    I feel if the question is not very well explained, but in summary, I look for a console where to see the logs in real time, just like it does intelij or android studio with the tab logcat.

    I would appreciate an explanation for beginners.

    Thanks in advance and greetings.

  • Sergio76
    Sergio76 over 5 years
    The translation of "logs tab" that they have made to Spanish is not clear. It's the reason why he had not linked that lash with the role he plays. Thank you very much.
  • Théo Champion
    Théo Champion almost 5 years
    Is it possible to read realtime log from the terminal as well?
  • Thijs Koerselman
    Thijs Koerselman over 4 years
    @Doug Stevenson Streaming logs to the terminal would be super helpful. The firebase console web interface for logs is pretty sluggish to work with, and it currently also doesn't place console.log from javascript into the "info" filter. I have to look at the debug messages too.
  • Doug Stevenson
    Doug Stevenson over 4 years
    If you have a feature request, please file that with Firebase support. support.google.com/firebase/contact/support
  • bjornl
    bjornl almost 4 years
    For CLI real-time logging, see Geoffery's answer, or mine. npx firebase-logging --project=YOUR_PROJECT --freq=1500
  • Taylor D. Edmiston
    Taylor D. Edmiston almost 4 years
    The issue I had with this approach is that there doesn't seem to be a follow / tail / similar option in the CLI log command, but this is possible in the GCP console.
  • MorenoMdz
    MorenoMdz about 3 years
    Thanks, the issue I have with this approach is that it refreshes the whole thing, is there a way to just watch for changes?
  • Sourav Kannantha B
    Sourav Kannantha B almost 3 years
    does this work if the function is running in emulator?