Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8714237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:32:54+00:00 2026-06-13T05:32:54+00:00

I have the following class: import cfg import sqlite3 import logging # logger =

  • 0

I have the following class:

import cfg
import sqlite3
import logging

# logger = logging ......

class Connection:
    def __init__(self):
        try:
            self.connection = sqlite3.connect(cfg.pathToDatabase)
            self.connection.row_factory = sqlite3.Row
            self.cursor = self.connection.cursor()
            logger.info("Connection to database at " + cfg.pathToDatabase + " successful")
        except Exception, e:
            logger.critical("Error connecting to database")
            logger.exception(e)

This class will be instantiated from multiple classes. In the following line:

logger.info("Connection to database at " + cfg.pathToDatabase + " successful")

I would like to log the class that called the __ init __ method in the Connection class. Is this possible ?

for example, based on the following:

class Child:
    def __init__(self):
        self.connection = Connection()

I would like to see this logged:

"Connection to database at data.sqlite successful from class: Child"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:32:56+00:00Added an answer on June 13, 2026 at 5:32 am

    Something like this?

    import traceback, sys
    
    class C:
        def __init__(self):
            try:
                raise StopIteration
            except StopIteration:
                tb = sys.exc_info()[2]
                stack = traceback.extract_stack(tb.tb_frame)
                f = stack[-2]
                print "I was called from %s %s (%s:%s)" % (f[2], f[3], f[0], f[1])
    
    class A:
        def __init__(self):
            self.c = C()
    
    def foo():
        A()
        return C()
    
    def main():
        C()
        foo()
    
    if __name__ == '__main__':
        main()
    

    Output:

    I was called from main C() (test.py:22)
    I was called from __init__ self.c = C() (test.py:15)
    I was called from foo return C() (test.py:19)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: from tkinter import * class MyApplication(Tk): def __init__(self): super().__init__()
In short, say I have the following: import multiprocessing class Worker(multiprocessing.Process): def __init__(self): multiprocessing.Process.__init__(self)
If you have the following class: class Foo(object): def __init__(name): self.name = name And
I have the following class in python class myTest: def __init__(self, str): self.str =
I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj):
I have the following decorator in a base class: class BaseTests(TestCase): @staticmethod def check_time(self,
In my GWT app I have the following model class: import com.google.gwt.user.client.rpc.IsSerializable; public class
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
I have the following class: #import SharedData.h static int selectedCountryIndex; static NSMutableArray *imageDataObjectsArray; @implementation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.