Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Logging in Cinema 4D

    Cinema 4D SDK
    sdk python
    3
    4
    969
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ?
      A Former User
      last edited by A Former User

      Hello,
      I'm trying to implement a logger for my plugin that saves logging messages from level DEBUG & higher. When I try using Python's logging module's basicConfig method, I'm unable to change the level or write the file. Here's the test script I'm using:

      import c4d, os, logging
      from c4d import storage
      
      def main():
          WRITEPATH = os.path.join(storage.GeGetC4DPath(c4d.C4D_PATH_DESKTOP), "MyLog.log")
          logging.basicConfig(filename=WRITEPATH, level=logging.DEBUG, filemode='w', format='%(name)s - %(levelname)s - %(message)s')
          logging.debug('This is a debug message')
          logging.info('This is an info message')
          logging.warning('This is a warning message')
          logging.error('This is an error message')
          logging.critical('This is a critical message')
      
      if __name__=='__main__':
          main()
      

      I've tried finding more information on Cinema 4D's logging and found this document on the LoggerInterface. I couldn't, however, find an example of how this is to be used. Can someone please provide a working example of saving log messages to a file in Cinema 4D? Thank you!

      1 Reply Last reply Reply Quote 0
      • lasselauchL
        lasselauch
        last edited by lasselauch

        Hi @blastframe,

        I'm using it currently more or less like this:

        The StreamHandler will print the result to the Python-Console and the FileHandler will write to disk. However, I'm also keen to know how to create my own LoggerInterface via Python. A simple example on this would be really appreciated.

        import c4d
        import os
        import logging
        
        def get_logger():
            plugin_name = "YourAwesomePlugin"
        
            #Logger for printing to Python-Console
            formatter1 = logging.Formatter('YourAwesomePlugin: [%(asctime)s] : %(module)s[%(lineno)s] : [%(levelname)s] "%(message)s"')
            handler = logging.StreamHandler()
            handler.setFormatter(formatter1)
            logger = logging.Logger(plugin_name)
            logger.addHandler(handler)
        
            #Logger for *.log file
            formatter2 = logging.Formatter('[%(asctime)s] : %(module)s[%(lineno)s] : [%(levelname)s] "%(message)s"')
            logpath = os.path.join(c4d.storage.GeGetC4DPath(c4d.C4D_PATH_DESKTOP), "MyLog.log")
            filehandler = logging.FileHandler(logpath)
            filehandler.setFormatter(formatter2)
            logger.addHandler(filehandler)
        
            return logger
        
        def main():
            logger = get_logger()
            
            logger.debug('This is a debug message')
            logger.info('This is an info message')
            logger.warning('This is a warning message')
            logger.error('This is an error message')
            logger.critical('This is a critical message')
        
        if __name__=='__main__':
            main()
        

        Cheers,
        Lasse

        ? 1 Reply Last reply Reply Quote 1
        • ?
          A Former User @lasselauch
          last edited by A Former User

          @lasselauch Absolutely awesome, thank you Lasse! This was just what I needed! 👍

          1 Reply Last reply Reply Quote 0
          • M
            m_adam
            last edited by m_adam

            Hi @blastframe unfortunately for the moment is not possible to implement its own logger in python.

            However, you could use any existing loggers.

            Here an example that writes into the Application (Default) Logger.

            import maxon
            
            txt = "My Wonderfull Text"
            defaultLogger = maxon.Loggers.Default()
            defaultLogger.Write(maxon.TARGETAUDIENCE.ALL, txt, maxon.MAXON_SOURCE_LOCATION(1), maxon.WRITEMETA.DEFAULT)
            

            Note that the print simply reroutes to the Python logger. But it can be useful to directly use the logger to write if you pass the write meta maxon.WRITEMETA.UI_SYNC_DRAW this way you can force a redraw of the console (slower) after each print, this can be used to avoid the limitation mentioned in the second part of this post.

            Cheers,
            Maxime

            MAXON SDK Specialist

            Development Blog, MAXON Registered Developer

            1 Reply Last reply Reply Quote 0
            • First post
              Last post