MxBlog

A script to set the lognode levels in Mendix

As of mendix 6, log levels can be configured through the runtime admin port before the actual loglevels exist. This enables you to create a script that will set all the required loglevel at once.

The following is a small python script that set the loglevels for ConnectionBus and ActionManager to trace and debug:

import requests, base64, json, sys

payload = {
    'action':'set_log_level',
    'params': {'nodes' : [
        { 'name':'ConnectionBus', 'level':'TRACE'},
        { 'name':'ActionManager', 'level':'DEBUG'}
        ], 'force':True}
}
usage = """
usage: python setlogs.py <admin password> [admin port]
When running from the Modeler, admin password can be retrieved using process explorer:
Determine M2EE_ADMIN_PASS for the java childprocess of Modeler.exe
"""

if len(sys.argv) < 2:
    print(usage)
    sys.exit()

url = 'http://localhost:{}/'.format(8090 if len(sys.argv) == 2 else sys.argv[2])
headers = {'X-M2EE-Authentication': base64.b64encode(sys.argv[1].encode('ascii'))}

response = requests.post(url,headers=headers, json=payload)
print(response.text)

You can run this script as follows:

C:\temp\LogDemoApp\python
λ python setlogs.py 87a32a3e-c6db-4bc8-9fa3-7cd5b108eaec 8090
{"feedback":{},"result":0}

You can see the result in the set log levels windows in the mendix modeler:

Set log levels window

There are more interesting ways to use the admin port of the Mendix runtime. Another script i use is a linux bash script for monitoring: it prints app statistics in a shell window, similar to vmstat.

#!/bin/bash
printf "%8s|%8s|%8s|%8s|%8s|%8s|%8s|%8s|%8s\n" "Cache" "Transact" "Select" "Delete" "Insert" "Update" "Anon ses" "Named s" "Nmd usr"
while true;
do
  printf "%8d %8d %8d %8d %8d %8d %8s %8s %8s\n" \
    $(curl -H "Accept: application/json" -H "Content-type: application/json" -H "X-M2EE-Authentication: MTIzNDU2Nzg5MHF3ZXJ0eQ==" -X POST -d "{'action':'runtime_statistics','params':{}}" http://localhost:9000/ 2>/dev/null | \
      ./jq '. | .feedback.cache.total_count,
                .feedback.connectionbus.transaction,
                .feedback.connectionbus.select,
                .feedback.connectionbus.delete,
                .feedback.connectionbus.insert,
                .feedback.connectionbus.update,
                .feedback.sessions.anonymous_sessions,
                .feedback.sessions.named_user_sessions,
                .feedback.sessions.named_users
           ' \
    )
  sleep 5;
done

This will produce the following output:

$ ./mxstat
   Cache|Transact|  Select|  Delete|  Insert|  Update|Anon ses| Named s| Nmd usr
       2    48685  1496481    85226    24347    33334        0        1        3
       2    48685  1496483    85226    24347    33334        0        1        3
       2    48685  1496484    85226    24347    33334        0        1        3