# Perspective Log events

**URL:** <https://forum.inductiveautomation.com/t/perspective-log-events/71814>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [March 30, 2023, 7:06am UTC](https://forum.inductiveautomation.com/t/perspective-log-events/71814 "2023-03-30T07:06:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [March 30, 2023, 7:06am UTC](https://forum.inductiveautomation.com/t/perspective-log-events/71814/1 "2023-03-30T07:06:53Z")

</div>

Hi

I would like to record the user's login and logout events in separate table in db rather than using audit events.  
I have a python lib 'auditing' as:

```auto
def Login():
	import datetime
	
	user = system.tag.readBlocking("[System]Client/User/Username")[0].value
	timestamp = system.date.now()
	

	vLocation =system.tag.readBlocking("[System]Client/Network/Hostname")[0].value
	query ="Insert into tvs_mes.tbl_logevents (user,Location,LoginTime,_createdDate,_createdUser)	values(?,?,?,?,?)"
	args= [user,vLocation,timestamp,timestamp,"ign"]
	system.db.runPrepUpdate(query,args)
	
	
def Logout():
	from time import strftime 
	user = system.tag.readBlocking("[System]Client/User/Username")[0].value
	timestamp = system.date.now()
	

	vLocation =system.tag.readBlocking("[System]Client/Network/Hostname")[0].value
	params={"iLogout":timestamp,"iMUser":"update","iUser":user,"iLocation":vLocation}
	system.db.runNamedQuery("Logging/UpdateLogout",params)

```

I tried calling out this function in session startup and shutdown. Unfortunately this did not work but the script worked as expected in the script console.  
I even tried inserting data to this table from trigger of audit event and the written trigger in mysql is:

```auto
CREATE TRIGGER audit_events_trigger
AFTER INSERT ON audit_events
FOR EACH ROW
BEGIN
    IF NEW.ACTION = 'Login Request' THEN
        INSERT INTO tbl_logevents (user, Location, LoginTime, _createdDate, _createdUser)
        VALUES (NEW.ACTOR, NEW.ACTOR_HOST, NEW.EVENT_TIMESTAMP, NOW(), 'ign');
    ELSEIF NEW.ACTION = 'Logout Response' THEN
        UPDATE tbl_logevents
        SET LogoutTime = NEW.EVENT_TIMESTAMP,
            ActiveTime = TIMEDIFF(NEW.EVENT_TIMESTAMP, LoginTime),
            _modifiedDate = NOW(),
            _modifiedUser = 'UPDATE'
        WHERE user = NEW.ACTOR AND Location = NEW.ACTOR_HOST AND LogoutTime IS NULL;
    END IF;
END;

```

Both the method did not worked as expected. Any ideas to achieve this?

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [April 1, 2023, 2:51am UTC](https://forum.inductiveautomation.com/t/perspective-log-events/71814/2 "2023-04-01T02:51:29Z")

</div>

Have you made anymore progress on this problem?

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [April 1, 2023, 4:30am UTC](https://forum.inductiveautomation.com/t/perspective-log-events/71814/3 "2023-04-01T04:30:58Z")

</div>

No Justin, I have tried writing the script on change script on auth.user.timestamp. Still not achieved the result.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [April 2, 2023, 3:35pm UTC](https://forum.inductiveautomation.com/t/perspective-log-events/71814/4 "2023-04-02T15:35:25Z")

</div>

Perspective does not use Tags like you're thinking it does. The `[System]Client` tags are for Vision. You should instead use a change script on `session.props.auth.user.username`.

Note that I specified a change script and not the onStartup Event of the session. This is because Session startup actually occurs before any login occurs because not every project requires login, and this event must still be the first thing that happens. If you watch the Perspective Session page of the Gateway, you'll notice that new sessions get started the moment a user opens a browser tab to the project - before they've logged in.

So the code to invoke your custom library functions would look something like this:

```auto
def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	if currentValue: # null check
        if currentValue.value: # new value is not null, meaning login has occurred
            auditing.Login(username=currentValue.value, userHost=self.session.props.host)
        else:
            auditing.Logout(username=previousValue.value, userHost=self.session.props.host)

```

Two more things to note here:

1. As you're not reading tags, you'll need to actually pass the values you're looking for into the functions you're using.
2. This form of auditing will miss events where a user simply closes their browser tab instead of logging out. When the session eventually times out and is closed out on the backend I don't believe the change script will execute, so you'll have no associated logout entry for the user in question.
