Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra Logging for LocalCKAN #214

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ multiple worker processes using the `-p` parameter. The jobs in progress,
the rate of job completion and any individual errors are shown on STDERR
while the jobs run.

There are no parallel limits when running against a CKAN on localhost.
There are no parallel limits when running against a CKAN on localhost.
When running against a remote site, there's a default limit of 3 worker processes.

The environment variables `CKANAPI_MY_SITES` and`CKANAPI_PARALLEL_LIMIT` can be
Expand Down Expand Up @@ -352,7 +352,7 @@ ua = 'ckanapiexample/1.0 (+http://example.com/my/website)'
demo = RemoteCKAN('https://demo.ckan.org', user_agent=ua)
packages = demo.action.package_search(q='+organization:sample-organization +res_format:GeoJSON +tags:geojson')
print(packages)
```
```

Many CKAN API functions can only be used by authenticated users. Use the
`apikey` parameter to supply your CKAN API key to `RemoteCKAN`:
Expand Down Expand Up @@ -463,6 +463,20 @@ anon = LocalCKAN(username='')
print(anon.action.status_show())
```

#### Extra Loggging

To enable extra info logging for the execution of LocalCKAN ckanapi commands, you can enable the config option in your CKAN INI file.

```
ckanapi.log_local = True
```

The output of the log will look like:

```
INFO [ckan.ckanapi] OS User <user> executed LocalCKAN: ckanapi <args>
```

### TestAppCKAN

A class is provided for making action requests to a
Expand Down
27 changes: 27 additions & 0 deletions ckanapi/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import os
from docopt import docopt
from pkg_resources import load_entry_point
import subprocess

from ckanapi.version import __version__
from ckanapi.remoteckan import RemoteCKAN
Expand All @@ -98,7 +99,10 @@
from ckanapi.cli.search import search_datasets
from ckanapi.cli.batch import batch_actions

from logging import getLogger

# explicit logger namespace for easy logging handlers
log = getLogger('ckan.ckanapi')
PYTHON2 = str is bytes

def parse_arguments():
Expand Down Expand Up @@ -130,6 +134,29 @@ def main(running_with_paster=False):
)
else:
ckan = LocalCKAN(username=arguments['--ckan-user'])
# log execution of LocalCKAN commands
from ckan.plugins.toolkit import config, asbool
if asbool(config.get('ckanapi.log_local')) and len(sys.argv) > 1:
cmd = ['who', 'am', 'i']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = proc.communicate()
if not out or err:
# fallback to whoami if `who am i` is empty or errored
cmd = ['whoami']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = proc.communicate()
if not out or err:
# cannot find user
out = '<unknown user>'
else:
# remove line breaks from whoami's
out = out.replace('\n', '').replace('\r', '')
JVickery-TBS marked this conversation as resolved.
Show resolved Hide resolved
# split the `who am i`
out = out.split()[0]
log.info('OS User %s executed LocalCKAN: ckanapi %s',
out, u' '.join(sys.argv[1:]))

stdout = getattr(sys.stdout, 'buffer', sys.stdout)
if arguments['action']:
Expand Down
Loading