Re: [python-win32] pywintypes.error: (5, 'RegSetValueEx', 'Access denied')

2018-03-15 Thread eryk sun
On Thu, Mar 15, 2018 at 8:44 AM, Robin Kluth  wrote:
>
> I use Python 2.7 on win10 and pywin32 223. I want to log messqages to the
> EventLog:
>
> dllname = os.path.dirname(__file__)
> ntl = NTEventLogHandler("Python Logging Test", dllname=dllname)

The Python module should not be used for dllname. This needs to be a
PE image (e.g. PYD, DLL, EXE) that has an embedded message table.
NTEventLogHandler currently just hard codes using event ID 1, which is
"%1\r\n" in win32evtlog.pyd or win32service.pyd. The "%1" insert gets
replaced by the message text when the log record is formatted.

In your case, just leave it as the default.

> Traceback (most recent call last):
>   File "fpp_user_import.py", line 44, in 
> ntl = NTEventLogHandler("Python Logging Test", dllname=dllname)
>   File "C:\Python27\lib\logging\handlers.py", line 987, in __init__
> self._welu.AddSourceToRegistry(appname, dllname, logtype)
>   File "C:\Python27\lib\site-packages\win32\lib\win32evtlogutil.py", line
> 42, in AddSourceToRegistry
> msgDLL)
> pywintypes.error: (5, 'RegSetValueEx', 'Zugriff verweigert')
>
> If I run the script with Admin privs, it is working.
>
> How to log as "normal" user? Am I missing something?

This should be enhanced in NTEventLogHandler. It's not the end of the
world if this key doesn't get created. The event viewer will just show
a warning that the event ID can't be found for the given source. Also,
when installed an application can request elevation to register the
event log source. Given this, I suggest you work around the problem
with a subclass that ignores this error. For example:

import logging
import logging.handlers

class NTEventLogHandler(logging.handlers.NTEventLogHandler):
__doc__ = logging.handlers.NTEventLogHandler.__doc__
def __init__(self, appname, dllname=None, logtype="Application"):
logging.Handler.__init__(self)
self.appname = appname
self.dllname = dllname
self.logtype = logtype
try:
import win32evtlogutil, win32evtlog, winerror
except ImportError:
print('The Python Win32 extensions for NT (service, event '
  'logging) appear not to be available.')
self._welu = None
return
self._welu = win32evtlogutil
self.deftype = win32evtlog.EVENTLOG_ERROR_TYPE
self.typemap = {
logging.DEBUG   : win32evtlog.EVENTLOG_INFORMATION_TYPE,
logging.INFO: win32evtlog.EVENTLOG_INFORMATION_TYPE,
logging.WARNING : win32evtlog.EVENTLOG_WARNING_TYPE,
logging.ERROR   : win32evtlog.EVENTLOG_ERROR_TYPE,
logging.CRITICAL: win32evtlog.EVENTLOG_ERROR_TYPE,
}
try:
win32evtlogutil.AddSourceToRegistry(appname, dllname, logtype)
except win32evtlogutil.error as e:
if e.winerror != winerror.ERROR_ACCESS_DENIED:
raise
___
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32


Re: [python-win32] pywintypes.error: (5, 'RegSetValueEx', 'Access denied')

2018-03-15 Thread Tim Roberts
Robin Kluth wrote:
>
> I use Python 2.7 on win10 and pywin32 223. I want to log messqages to
> the EventLog:
>
> dllname = os.path.dirname(__file__)
> ntl = NTEventLogHandler("Python Logging Test", dllname=dllname)
> logger = logging.getLogger("")
> logger.setLevel(logging.DEBUG)
> logger.addHandler(ntl)
>
> logger.error("This is a '%s' message", "Error")
>
> But I get:
> ...
> pywintypes.error: (5, 'RegSetValueEx', 'Zugriff verweigert')
>
> If I run the script with Admin privs, it is working.
>
> How to log as "normal" user? Am I missing something?

You can't.  If you want to log to the event log, you'll need
privileges.  Each app must register as an "event source".  Doing so
requires writing to the HKEY_LOCAL_MACHINE part of the registry, and
write requires admin privileges.

Technically speaking, it would be possible to register that source once
in an elevated process and then use it without re-registering, but the
current code does not support that.

-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.

___
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32


[python-win32] pywintypes.error: (5, 'RegSetValueEx', 'Access denied')

2018-03-15 Thread Robin Kluth

Hi :)

I use Python 2.7 on win10 and pywin32 223. I want to log messqages to 
the EventLog:


dllname = os.path.dirname(__file__)
ntl = NTEventLogHandler("Python Logging Test", dllname=dllname)
logger = logging.getLogger("")
logger.setLevel(logging.DEBUG)
logger.addHandler(ntl)

logger.error("This is a '%s' message", "Error")




But I get:

Traceback (most recent call last):
  File "fpp_user_import.py", line 44, in 
ntl = NTEventLogHandler("Python Logging Test", dllname=dllname)
  File "C:\Python27\lib\logging\handlers.py", line 987, in __init__
self._welu.AddSourceToRegistry(appname, dllname, logtype)
  File "C:\Python27\lib\site-packages\win32\lib\win32evtlogutil.py", 
line 42, in AddSourceToRegistry

msgDLL)
pywintypes.error: (5, 'RegSetValueEx', 'Zugriff verweigert')



If I run the script with Admin privs, it is working.

How to log as "normal" user? Am I missing something?

Thanks in advance!
___
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32