0

I want to save the logs into a file/blob in a specific folder of an azure container using python.

I've used this code;

today = date.today()

logging.basicConfig(filename='http://127.0.0.1:10000/devstoreaccount1/cloudops-resources/logs/{}_logs.log'.format(today),
                filemode='a',
                format='%(asctime)s,%(msecs)d %(name)s %(message)s',
                datefmt='%H:%M:%S',
                level=logging.INFO,
                force=True)

But it gives an error.

Executed 'Functions.BlobTrigger' (Failed, Id=8863e10e-2cb6-4a32-8f3a-2486823740cb, Duration=30ms) [2023-04-19T11:28:58.348Z] System.Private.CoreLib: Exception while executing function: Functions.BlobTrigger. System.Private.CoreLib: Result: Failure
Exception: OSError: [Errno 22] Invalid argument: 'D:\Automization of Invoice Compare\Service Projects-1\Application Cloud\Customers\InvoiceCompare\http:\127.0.0.1:10000\devstoreaccount1\cloudops-resources\logs\2023-04-19_logs.log' Stack: File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 452, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "C:\Users\Asu\AppData\Local\Programs\Python\Python39\lib\concurrent\futures\thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 718, in run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\extension.py", line 215, in raw_invocation_wrapper result = function(**args) File "D:\Automization of Invoice Compare\Service Projects-1\Application Cloud\Customers\InvoiceCompare\BlobTrigger_init.py", line 20, in main logging.basicConfig(filename='http://127.0.0.1:10000/devstoreaccount1/cloudops-resources/logs/{}logs.log'.format(today), File "C:\Users\Asu\AppData\Local\Programs\Python\Python39\lib\logging_init.py", line 2003, in basicConfig h = FileHandler(filename, mode, File "C:\Users\Asu\AppData\Local\Programs\Python\Python39\lib\logging_init.py", line 1146, in init StreamHandler.init(self, self.open()) File "C:\Users\Asu\AppData\Local\Programs\Python\Python39\lib\logging_init.py", line 1175, in _open return open(self.baseFilename, self.mode, encoding=self.encoding,

Can someone please help me to resolve this?

1 Answer 1

0

How to add logs to a file/blob in an azure container with basicConfig

Using basicConfig you can add a log file to storage account, but you are wrong in sending URL in place of PATH of the log File and this is working if i am giving local path too.

Below is the code(updated your code):

import logging
from azure.storage.blob import BlobServiceClient

c = 'DefaultEndpointsProtocol=https;AccountName=rithwik2;AccountKey=PVggwes02w==;EndpointSuffix=core.windows.net'
bsc = BlobServiceClient.from_connection_string(c)
cn = "emocon"
bn = "emolog"
cc = bsc.get_container_client(cn)
cb = cc.get_blob_client(bn)
logging.info("Hello Rithwik")
logging.basicConfig(level=logging.INFO,
                    format="%(asctime)s [%(name)s] %(message)s",
                    handlers=[logging.StreamHandler(),
                              logging.handlers.WatchedFileHandler("emolog.log")])
with open("emolog.log", "rb") as d:
    cb.upload_blob(d, overwrite=True)

enter image description here

Output:

enter image description here

So you can integrate the above code into your function app.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried it, the file is created but it is empty.(btw I'm trying this with azure storage explorer.)
Oh, but it wasn't empty for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.