Home > Mobile >  Implement the *logger* function which will prepare data for logging
Implement the *logger* function which will prepare data for logging

Time:01-05

I have a task to Implement the logger function which will prepare data for logging.It should have default service configuration {serviceName: 'global', serviceId: 1}
Some examples:

 // {"3-0":"[auth_service] Wrong email","3-1":"[auth_service] Wrong password","3-2":"[auth_service] Success login"}
  logger(['Wrong email', 'Wrong password', 'Success login'], {serviceName: 'auth_service', serviceId: 3});

  // {"1-0":"[global] Fatal error","1-1":"[global] Data corrupted"}
  logger(['Fatal error', 'Data corrupted']);

It should be written using Typescript. I started to make this function but i dont know how to make it look like the example or like test samples.. This is what i did till now :

function logger(data: [], serviceInfo: {}) {
    //It should have default service configuration `{serviceName: 'global', serviceId: 1}`
    if (serviceInfo == undefined) {
        serviceInfo = {
            serviceName: "global",
            serviceId: 1,
        };
    } else {
     let obj = {
        
     }
     return obj;
    }
}

And unit tests:

describe('logger', () => {
    it('should log user events', () => {
        const result = logger(['User Event 1', 'User Event 2'], {serviceName: 'user', serviceId: 12});
        const expected = {
            '12-0': '[user] User Event 1',
            '12-1': '[user] User Event 2',
        };
        assert.deepStrictEqual(result, expected)
    });

    it('should log auth events', () => {
        const result = logger(['Event 1', 'Event 2', 'Event 3'], {serviceName: 'auth', serviceId: 2});
        assert.strictEqual(result['2-1'], '[auth] Event 2')
    });

    it('should log global events', () => {
        const result = logger(['Event 1', 'Event 2', 'Event 3']);
        assert.strictEqual(result['1-2'], '[global] Event 3')
    });
});

I thought about pushing the array into a different object,but i dont know how to make that depending on serviceId to appear like:

{
'12-0':'[user] User event 1'
}

where 12 is the service id, 0 is the array item index, [user] is the serviceName and the user event 1 is the array item.. I'm new in Javascript and Typescript and idk all the nuances. Could you help me please? Thanks in advance!

Edit: I tried a little and came up with this function :

function logger(data: [], serviceInfo: {}) {
    //It should have default service configuration `{serviceName: 'global', serviceId: 1}`
  serviceInfo = {
      serviceName : 'Gigi',
      serviceId : 0
  }

    if (serviceInfo == undefined) {
        serviceInfo = {
            serviceName: "global",
            serviceId: 1,
        };
    } 
    let obj={}
    //obj[serviceInfo.serviceId.value   ]

    data.forEach(function (value, i) {
        //console.log('%d: %s', i, value);
        obj[serviceInfo.serviceId.value  '-'  i] = '['   serviceInfo.serviceName.value   ']'   value
    });
    console.log(obj)
}

But in console i get :

{
  'undefined-0': '[undefined]User Event 1',
  'undefined-1': '[undefined]User Event 2'
}
{
  'undefined-0': '[undefined]Event 1',
  'undefined-1': '[undefined]Event 2',
  'undefined-2': '[undefined]Event 3'
}
{
  'undefined-0': '[undefined]Event 1',
  'undefined-1': '[undefined]Event 2',
  'undefined-2': '[undefined]Event 3'
}

How to acces serviceName and serviceId if data is get from input not from function?

CodePudding user response:

I solved the function,this way :

function logger(data: [], serviceInfo: {}) {
    if (serviceInfo == undefined) {
        serviceInfo = {
            serviceName: "global",
            serviceId: 1,
        };
    }
    let obj = {};

    data.forEach(function (value, i) {
        //console.log('%d: %s', i, value);
        obj[serviceInfo.serviceId   "-"   i] =
            "["   serviceInfo.serviceName   "] "   value;
    });
    return obj
}

The problem upthere when i edited the question is that i incorectly accesed object property to get the value,so,now the function passes all test units.

  •  Tags:  
  • Related