Link to home
Start Free TrialLog in
Avatar of WAS
WASFlag for United States of America

asked on

provide a function for logging in jython

HonorGod,
 How are you, i hope you are doing good.

 I need help for the following question
 Please provide me a logging function, where i can reuse it for calling it in my other functions, like i have lot of functions and class for doing various task like copying the jboss zip file,
 unzipping it, creating clusters, backing up config xml files and modifying them and few other tasks my jython script will do, so i should be able call the logging function provided by you in functions
 i write. If needed i can import it as module.

Please let me know if you need more info
Avatar of HonorGod
HonorGod
Flag of United States of America image

I'm doing ok, thanks for asking.  ;-)  I hope that you too are doing well.

Are you asking for logging along the lines of what is discussed here?
http://www.jython.org/docs/library/logging.html

Or something else?
Avatar of WAS

ASKER

yeah i found a similar doc in python, yes please provide logging facility for the attached file,
http://docs.python.org/library/logging.html?highlight=import%20logging
the log should rotate every time i run the script with time stamp.


postconfig1.py
Be careful though.  Your question specifically identifies Jython.

What version of Jython are you using?

Is it bundled with another product?

If so, which one?

e.g., the version of Jython used by the wsadmin tool/utility in the WebSphere Application Server is at version 2.1, not 2.3 as is identified by your link to the Python logging facility.
You can find out by starting an interactive session, and executing the following

import sys;
print sys.version;

Open in new window

Avatar of WAS

ASKER

sorry for the confusion, after reading python and jython books, i came to know there is not much difference between python and jython,
the only major difference is in jython we can write java classes

Also i am not using the jython scripting for WebSphere env, i am using it for automating the installing and manual configurations in jboss env.

In my redhat linux environment, i am using jython standalone jar file sometimes and sometimes i am using jython full installation.
the version i am using is jython2.5.2
[webadmin@fedora scripts]$ java -jar jython.jar
Jython 2.5.2rc3 (Release_2_5_2rc3:7184, Jan 10 2011, 22:54:57)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_23
Type "help", "copyright", "credits" or "license" for more information.
>>>

all sample code i get from python book is getting executed in this jython interpreter
Excellent!  Thanks for that clarification.

So, how else can I help?
Avatar of WAS

ASKER


Please provide answer to my initial question, provide logging function and give me an example how i can call it from different functions, remember my logging function should be
 able to rotate logs with time stamp, every time i restart my script , use the attached the python script.

By the way the i didn't written this script, i am not that expert yet :)
engine.txt
ok, this is going to require some clarification.  Please forgive me for my lack of understanding.

> Please provide me a logging function, where i can reuse it for calling it in my other functions,
> like i have lot of functions and class for doing various task like copying the jboss zip file,

I thought that you had "found" it with the logging module mentioned above:
  https://www.experts-exchange.com/questions/26853472/provide-a-function-for-logging-in-jython.html?anchorAnswerId=35003657#a35003657

I will modify the example engine.txt after I get the answers to the following questions

Q: What, exactly, do you mean by "able to rotate logs with time stamp"?

Q: Do you have different "levels" of severity for the messages to be written?

Q: Do you want the name of the log file to be passed in as a script parameter, or hard coded in the script, or do you want the script to prompt the user for the filename?

> i am not that expert yet :)

  Don't worry about it.  We're here to help.

Thanks for the clarifications!
Avatar of WAS

ASKER

i passed that link for only reference, i don't know how to implement that in the current attached engine.txt,

Q: What, exactly, do you mean by "able to rotate logs with time stamp"?
like below, don't worry about timestamp
logging_rotatingfile_example.out
logging_rotatingfile_example.out.1
logging_rotatingfile_example.out.2
logging_rotatingfile_example.out.3
logging_rotatingfile_example.out.4
logging_rotatingfile_example.out.5

Q. o you have different "levels" of severity for the messages to be written?
Debug level is good for now

Q. Do you want the name of the log file to be passed in as a script parameter, or hard coded in the script, or do you want the script to prompt the user for the filename?
hardcoded in script

Here is the another python script i attached, where i defined all the classes
tags.txt
Avatar of pepr
pepr

I do not want to mix to your communication.  Just one suggestion.  Define your own function like log(s) (possibly in some module like log.py).  It may be easier for you to use the simplified interface.

You can implement the function so that anything else is hidden behind the scene -- including the filename choice, the implementation possibly via the standard logging module or via some simpler "append line to the file".
I also suggest to compose the date to the log filename and erase the logs older than... It depends on situation.  In my opinion, it does not help too much to reuse say 10 names of the files.  It is easier and more practical to generate new filenames that are better bound via their name to the time of their creations.  It should be as easy as possible to say what the name of the log file should be used "just now".
Avatar of WAS

ASKER

HonorGod,

Any updates!!! i am waiting on your input
tags.py is non-trivial.  As such, your request to add logging raises some questions, e.g.,

Q: In order to to better understand the code that you provided, is the source for "util.py" available?

Q: How, exactly, (and why) do you want logging to be added?

Q: Are you simply trying to understand how the code works?

Q: Or, are you trying to make the code more robust / maintainable by having the logging as an integral part of the code?

Q: From where did this code originate?  [this is not super important, I'm just curious]
Avatar of WAS

ASKER

i have the util.py but i don't want to complicate any further, i attached another file which is written by you only earlier, please write a seperate module for logging and import it into postconfig.py ( attached to ticket),
make all functions in postconfig.py to through output to log when they get executed. Simple to say is , what ever i see in the console when i run postconfig.py, i should see them in logs also. And i need new log everytime i
execute my python script

I was trying to write log module my self, but i can't make it work,
import glob
import logging
import logging.handlers


LOG_FILENAME = 'example.log'
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=20, backupCount=5)

def jboss_log(LOG_FILENAME):
    my_logger.addHandler(handler)



Yes this is important for me, bcz my project manager is waiting on this, this is kind learn and work project.
   
postconfig1.txt
It's not your fault.

The tutorial has some mistakes. ;-(

This may be simple enough for your situation:
>>> import logging
>>>
>>> def g() :
...   1/0
...
>>>
>>> def f() :
...   logging.debug( 'Enter: f()' )
...   try :
...     g()
...   except :
...     logging.exception( 'Houston, we have a problem.' )
...   logging.debug( ' Exit: f()' )
...
>>> # Log everything, writing to stderr by default
>>> logging.basicConfig( level=logging.DEBUG )
>>>
>>> f()
DEBUG:root:Enter: f()
ERROR:root:Houston, we have a problem.
Traceback (most recent call last):
  File "<stdin>", line 4, in f
  File "<stdin>", line 2, in g
ZeroDivisionError: integer division or modulo by zero
DEBUG:root: Exit: f()
>>>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HonorGod
HonorGod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ah, I found the mistake I was making with the example from the tutorial.

When I put the following code into a file, and executed it with Jython, the output is:
------------------------------------------------------------
logging_rotatingfile_example.out
logging_rotatingfile_example.out.1
logging_rotatingfile_example.out.2
logging_rotatingfile_example.out.3
logging_rotatingfile_example.out.4
logging_rotatingfile_example.out.5
------------------------------------------------------------
as expected... as long as you expect log file rotation to include the renaming of
existing files when log file rollover occurs.


import glob
import logging
import logging.handlers

LOG_FILENAME = 'logging_rotatingfile_example.out'

# Set up a specific logger with our desired output level
my_logger = logging.getLogger( 'MyLogger' )
my_logger.setLevel( logging.DEBUG )

# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler( LOG_FILENAME, maxBytes=20, backupCount=5 )

my_logger.addHandler( handler )

# Log some messages
for i in range(20):
    my_logger.debug('i = %d' % i)

# See what files are created
logfiles = glob.glob('%s*' % LOG_FILENAME)

for filename in logfiles:
    print filename

Open in new window

I believe that what you don't appear to realize, from your post:

https://www.experts-exchange.com/questions/26853472/provide-a-function-for-logging-in-jython.html?anchorAnswerId=35020808#a35020808

is that this code:

def jboss_log(LOG_FILENAME):
    my_logger.addHandler(handler)

doesn't generate, or log any output.

As I said earlier, it is non-trivial, and will take quite a bit of explanation.

Let me take a look at tags.py, as provided in https://www.experts-exchange.com/questions/26853472/provide-a-function-for-logging-in-jython.html?anchorAnswerId=35010466#a35010466
to see how I would add logging to it.
Avatar of WAS

ASKER

Thanks for trying, i will open another ticket with clean  and fresh start.
Thank you so much for the grade & points.

Good luck & have a great day.

Unfortunately, I'm home sick today, and having difficulty concentrating. ;-(
Avatar of WAS

ASKER

oh man i am really sorry to put pressure on you. Take rest...Hope you will get well soon.

My tickets can wait. :)