can i add something to the pyramid logging ?

124 views
Skip to first unread message

Guohuang Chen

unread,
May 26, 2017, 11:04:05 PM5/26/17
to pylons-discuss
can i add something to the pyramid logging ? such as session['login_name'] remote-ip ...

Michael Merickel

unread,
May 30, 2017, 11:20:15 PM5/30/17
to Pylons
Yes, but to do so you need to read the logging documentation and come up with an approach that works for you in the Python stdlib logging module [1]. One option is to configure a filter and another is a logging adapter or even a custom formatter. The simplest approach is probably a custom formatter that uses pyramid.threadlocal.get_current_request() to inject extra formatting options into the record but I do not have any sample code to give you. The most difficult approach is to add a filter because it is not possible to configure filters from the ini file.


On Fri, May 26, 2017 at 10:04 PM, Guohuang Chen <chen.g...@gmail.com> wrote:
can i add something to the pyramid logging ? such as session['login_name'] remote-ip ...

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/4a758c61-1f22-460a-bde9-a4e047d8e389%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

tonthon

unread,
May 31, 2017, 10:15:53 AM5/31/17
to pylons-...@googlegroups.com



Le 27/05/2017 à 05:04, Guohuang Chen a écrit :
can i add something to the pyramid logging ? such as session['login_name'] remote-ip ...
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Message has been deleted
Message has been deleted

Guohuang Chen

unread,
May 31, 2017, 10:01:02 PM5/31/17
to pylons-discuss

def emit(self, record):
request = get_current_request()
record.ip = get_ip(request)
record.user = get_user(request)
super(CustomStreamHandler, self).emit(record)

the request is always None ?


在 2017年5月31日星期三 UTC+8下午10:15:53,tonthon写道:

Jeff Dairiki

unread,
Jun 1, 2017, 12:52:46 PM6/1/17
to pylons-...@googlegroups.com
On Fri, May 26, 2017 at 8:04 PM, Guohuang Chen <chen.g...@gmail.com> wrote:
can i add something to the pyramid logging ? such as session['login_name'] remote-ip ...

You might also look at the pyramid-log package.  I think it will do what you want.

It provides a custom log formatter which adds the pyramid request to the log record, and also adds some magic syntax so that one can interpolate bits accessible from the request with a format string like:

%(asctime)s %(request.session.login_name)s %(request.client_addr)s %(message)s

Michael Merickel

unread,
Jun 1, 2017, 1:07:58 PM6/1/17
to Pylons

On Thu, Jun 1, 2017 at 11:52 AM, Jeff Dairiki <dai...@dairiki.org> wrote:
You might also look at the pyramid-log package.  I think it will do what you want.

Jeff, this library looks very nice. You should add it to https://trypyramid.com/resources-extending-pyramid.html

Steve Piercy

unread,
Jun 1, 2017, 1:42:22 PM6/1/17
to pylons-...@googlegroups.com
On 6/1/17 at 12:07 PM, mmer...@gmail.com (Michael Merickel) pronounced:

> On Thu, Jun 1, 2017 at 11:52 AM, Jeff Dairiki <dai...@dairiki.org> wrote:
>
> > You might also look at the pyramid-log
> > <https://pypi.python.org/pypi/pyramid-log> package. I think it will do
> > what you want.
> >
>
> Jeff, this library looks very nice. You should add it to
> https://trypyramid.com/resources-extending-pyramid.html

Please do!
https://github.com/Pylons/trypyramid.com/issues/202

--steve

------------------------
Steve Piercy, Soquel, CA

Jeff Dairiki

unread,
Jun 1, 2017, 7:06:27 PM6/1/17
to pylons-...@googlegroups.com
On Thu, Jun 1, 2017 at 10:42 AM, Steve Piercy <steve.pi...@gmail.com> wrote:
On 6/1/17 at 12:07 PM, mmer...@gmail.com (Michael Merickel) pronounced:

> On Thu, Jun 1, 2017 at 11:52 AM, Jeff Dairiki <dai...@dairiki.org> wrote:
>
> > You might also look at the pyramid-log
> > <https://pypi.python.org/pypi/pyramid-log> package.  I think it will do
> > what you want.
> >
>
> Jeff, this library looks very nice. You should add it to
> https://trypyramid.com/resources-extending-pyramid.html

Please do!
https://github.com/Pylons/trypyramid.com/issues/202

Thanks!  Will do, as soon as I have a spare moment...

tonthon

unread,
Jun 12, 2017, 6:30:01 AM6/12/17
to pylons-...@googlegroups.com
That's a cool one, thanks for sharing.
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.

Guohuang Chen

unread,
Jun 16, 2017, 5:43:18 AM6/16/17
to pylons-discuss
you can catch the request client ip using pyramid_log ?
i use config in development.ini by the https://pypi.python.org/pypi/pyramid-log
[formatter_pyramid]
# NB: Here is the interesting part!
class = pyramid_log.Formatter
format = %(asctime)s %(request.method|no request)s %(request.path_qs|)s
         %(levelname)-5.5s [%(name)s] %(message)s
and what i caught the errors like this:


2017-06-16 17:37:10,590 no request 
ERROR [waitress] Exception when serving /saveData
Traceback (most recent call last):
  File "d:\pyramid64_py36\lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context
    context)
  File "d:\pyramid64_py36\lib\site-packages\sqlalchemy\engine\default.py", line 470, in do_execute
    cursor.execute(statement, parameters)
cx_Oracle.DatabaseError: ORA-00907: 缺失右括号


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "d:\pyramid64_py36\lib\site-packages\waitress\channel.py", line 338, in service
    task.service()
  File "d:\pyramid64_py36\lib\site-packages\waitress\task.py", line 169, in service
    self.execute()
  File "d:\pyramid64_py36\lib\site-packages\waitress\task.py", line 399, in execute
    app_iter = self.channel.server.application(env, start_response)
  File "d:\pyramid64_py36\lib\site-packages\pyramid\router.py", line 233, in __call__
    response = self.invoke_subrequest(request, use_tweens=True)
  File "d:\pyramid64_py36\lib\site-packages\pyramid\router.py", line 208, in invoke_subrequest
    response = handle_request(request)
  File "d:\pyramid64_py36\lib\site-packages\pyramid\tweens.py", line 62, in excview_tween
    reraise(*attrs['exc_info'])
  File "d:\pyramid64_py36\lib\site-packages\pyramid\compat.py", line 148, in reraise
    raise value
  File "d:\pyramid64_py36\lib\site-packages\pyramid\tweens.py", line 22, in excview_tween
    response = handler(request)
  File "d:\pyramid64_py36\lib\site-packages\pyramid_tm\__init__.py", line 119, in tm_tween
    reraise(*exc_info)
  File "d:\pyramid64_py36\lib\site-packages\pyramid_tm\compat.py", line 15, in reraise
    raise value
  File "d:\pyramid64_py36\lib\site-packages\pyramid_tm\__init__.py", line 98, in tm_tween
    response = handler(request)
  File "d:\pyramid64_py36\lib\site-packages\pyramid\router.py", line 155, in handle_request
    view_name
  File "d:\pyramid64_py36\lib\site-packages\pyramid\view.py", line 612, in _call_view
    response = view_callable(context, request)
  File "d:\pyramid64_py36\lib\site-packages\pyramid\viewderivers.py", line 438, in rendered_view
    result = view(context, request)
  File "d:\pyramid64_py36\lib\site-packages\pyramid\viewderivers.py", line 147, in _requestonly_view
    response = view(request)
  File "d:\pyramid64_py36\gds_zf_dev\gds_zf_dev\views\home.py", line 275, in view_savedata
    return savedata.view_savedata(request)
  File "d:\pyramid64_py36\gds_zf_dev\gds_zf_dev\views\savedata.py", line 80, in view_savedata
    return func_savedata(request)
  File "d:\pyramid64_py36\gds_zf_dev\gds_zf_dev\views\dtcx.py", line 35, in view_savedata
    return func_saveDtcx(request)
  File "d:\pyramid64_py36\gds_zf_dev\gds_zf_dev\views\dtcx.py", line 202, in func_saveDtcx
    {'b1': '', 'b2': '', 'b3': '', 'b4': '', 'b5': '', 'b6': '', 'b7': '', 'b99': ''}
  File "d:\pyramid64_py36\lib\site-packages\sqlalchemy\orm\session.py", line 1107, in execute
    bind, close_with_result=True).execute(clause, params or {})
  File "d:\pyramid64_py36\lib\site-packages\sqlalchemy\engine\base.py", line 945, in execute
    return meth(self, multiparams, params)
  File "d:\pyramid64_py36\lib\site-packages\sqlalchemy\sql\elements.py", line 263, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "d:\pyramid64_py36\lib\site-packages\sqlalchemy\engine\base.py", line 1053, in _execute_clauseelement
    compiled_sql, distilled_params
  File "d:\pyramid64_py36\lib\site-packages\sqlalchemy\engine\base.py", line 1189, in _execute_context
    context)
  File "d:\pyramid64_py36\lib\site-packages\sqlalchemy\engine\base.py", line 1393, in _handle_dbapi_exception
    exc_info
  File "d:\pyramid64_py36\lib\site-packages\sqlalchemy\util\compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "d:\pyramid64_py36\lib\site-packages\sqlalchemy\util\compat.py", line 186, in reraise
    raise value.with_traceback(tb)
  File "d:\pyramid64_py36\lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context
    context)
  File "d:\pyramid64_py36\lib\site-packages\sqlalchemy\engine\default.py", line 470, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.DatabaseError: (cx_Oracle.DatabaseError) ORA-00907: 缺失右括号
 [SQL: '...

在 2017年6月12日星期一 UTC+8下午6:30:01,tonthon写道:
Message has been deleted

Guohuang Chen

unread,
Jun 22, 2017, 9:38:04 PM6/22/17
to pylons-discuss
i think it is a waitress problem . the waitress  thread execute the sqlchemy does not have a request attr. is it?

在 2017年5月27日星期六 UTC+8上午11:04:05,Guohuang Chen写道:

Bert JW Regeer

unread,
Jun 23, 2017, 12:50:46 AM6/23/17
to pylons-...@googlegroups.com
waitress doesn’t execute sqlalchemy or have a separate thread for it, it sends the request into your WSGI application.

Somewhere your application is raising an exception. According to "缺失右括号” that is a missing parentheses on a SQL statement.

Bert
> --
> You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
> To post to this group, send email to pylons-...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/4fa25eb2-68b3-4dea-88a7-5ad4d271add8%40googlegroups.com.

Guohuang Chen

unread,
Jun 23, 2017, 3:27:28 AM6/23/17
to pylons-discuss
i delete some sql section to raise this exception to test if the pyramid can log the request's client ip and the session when my script have some error.

在 2017年6月23日星期五 UTC+8下午12:50:46,Bert JW Regeer写道:
Reply all
Reply to author
Forward
0 new messages