Skip to content

Unable to import logger #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fjahr opened this issue Nov 13, 2023 · 6 comments
Closed

Unable to import logger #161

fjahr opened this issue Nov 13, 2023 · 6 comments

Comments

@fjahr
Copy link

fjahr commented Nov 13, 2023

I am trying to use the logger, just like in the example given here: https://firebase.google.com/docs/functions/writing-and-viewing-logs?gen=2nd

But when I try to deploy the function I get the following error message:

    from firebase_functions import logger
ImportError: cannot import name 'logger' from 'firebase_functions'

I am using the latest version (0.1.2) but indeed when I go into the venv folder where it is installed I don't see the logger file included. Any idea what is going on?

@matthewnaples
Copy link

I'm also having this problem. The following code is broken, but when removing the logger import and logger.info(...) call, the code works and successfully deploys

from firebase_functions import https_fn, logger

from firebase_admin import initialize_app, firestore

initialize_app()


@https_fn.on_request()
def write_data(req: https_fn.Request) -> https_fn.Response:
    """This function will take the request and write its data to cloud firestore in a collection called 'submissions'"""
    #log that the function has been called
    logger.info("write_data function called")

    # Get the data from the request
    data = req.get_json()

    # Get the firestore client
    firestore_client = firestore.client()

    # Write the data to the firestore
    firestore_client.collection("submissions").add(data)

    # Return a response
    return https_fn.Response("OK", 200)

@jaclar
Copy link

jaclar commented Nov 23, 2023

Seems like the logger feature was merged after the last release (0.1.2): #121
So I hope it is part of the next release.

Anyway, strange that it is already mentioned in the official documentation.

@MWals
Copy link

MWals commented Nov 24, 2023

Too bad that the logger can not be imported in version 0.12, looking forward to the next release.

Meanwhile, this is my workaround to get logs inside the emulator:

import logging

logger = logging.getLogger('cloudfunctions.googleapis.com%2Fcloud-functions')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())


@https_fn.on_request()
def text_fxn(req: https_fn.Request) -> https_fn.Response:
logger.log(msg='Log from function"', level=logging.INFO)
    return https_fn.Response("OK", status=200)

Output:

i  functions: Beginning execution of "us-central1-on_packing_box_closed"
>  Log from function
i  functions: Finished "us-central1-on_packing_box_closed" in 1.831333ms

@sandpat
Copy link

sandpat commented Dec 1, 2023

I think you can just copy paste the following file in your code for time being:

https://raw.githubusercontent.com/firebase/firebase-functions-python/3662d07c0f84cc19fdf5d548553fb9c7b5dd00d7/src/firebase_functions/logger.py

tomhmoses added a commit to tomhmoses/Strava-UKC that referenced this issue Dec 10, 2023
tomhmoses added a commit to tomhmoses/Strava-UKC that referenced this issue Dec 13, 2023
tomhmoses added a commit to tomhmoses/Strava-UKC that referenced this issue Dec 13, 2023
@TRON-N
Copy link

TRON-N commented Jan 2, 2024

I came here looking for a solution to this issue, but it seems to be fixed in 0.2.0 #167. Thanks Everyone!

@exaby73
Copy link
Contributor

exaby73 commented Jan 8, 2024

Since this issue is fixed in the latest version (sorry about that), I feel safe to close this issue. Thanks for your patience everyone :)

@exaby73 exaby73 closed this as completed Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants