org.slf4j.Logger's isTraceEnabled() method is returning false , but in logback.xml log level is set to "level="TRACE" - logback

I have set log level to "TRACE" in logback.xml.
In my java class i have code like,
if (org.slf4j.Logger.isTraceEnabled()) {
org.slf4j.Logger.trace("Log tracing is enabled");
}
But the if loop condition is returning false.
Can anyone help me understand how it works? What i'm doing wrong?
Edit: Created Logger object like,
org.slf4j.LoggerFactory.getLogger(Test.class);
Is the issue is because i'm using logback and creating object using org.slf4j?

Related

need help withself reference loop when serialize linq

I know this question was asked before but I couldn't find any solution for my issue.
I am developing a WebAPI with more than 10 Controllers which their methods access a server DB.
I am using Linq2SQL to write the queries and using Json to serialize the return to send it back to my application.
The problem is no matter how simple is the query it returns the self reference loop when serialize and this is happening in all controller methods. See one example below:
var retitems = dtcxapi.ListItems.AsQueryable()
.Where(i => i.IsActive == true && i.ListName.ToLower() == listName.ToLower()).ToList();
where dtcxapi is my DataContext and ListItems is my table.
When serialize it shows: Self referencing loop detected with type 'BV.IMSWEBAPI.User'. Path '[0].User1.Users1'.
But as I said this error will occur for any query in any controller methods. I tried already to use the ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore in mWeb config but it didn't fix.
Any help will be really appreciated.
Thanks
The only true way to fix this is to not return your Linq objects and instead return a DTO/Model that is not tied to your database. If you are returning your database objects, you will always run into self referencing loops because of Navigation properties.
You haven't mentioned if you are using .NET Core or .NET Framework, but if it's Core, it won't use web.config at all and instead you should modify your startup method :
services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
But again, this is a bandaid, the correct solution is to use DTOs
More info : https://dotnetcoretutorials.com/2020/03/15/fixing-json-self-referencing-loop-exceptions/

Bad Method Call Exception

Here is my route
Route::get('desktops/{desktop}','DesktopsController#getdesktop');
Here is my controller method
public function getdesktop($identifier)
{
$desktop = Desktop::all()->where('identifier',$identifier);
$casings = $desktop->casings();
dd($casings);
}
I have added the App\Desktop, here is my casings method defined inside Desktop Class/Model
public function casings()
{
return $this->hasMany('App\Casing');
}
Now it seems to be working on tinker, but when I open it in my browser it throws an exception of method not found or to be exact BadMethodCallException, even though I have method defined inside the class. I can't seem to figure out the problem.
When you define the relationship in your model it is carried in to the eloquent object as a property rather than a function inside the collection.
As such you need to access it without the ():
$casings = $desktop->casings;
You can find out more about accessing the relationships in the docs.
I was using this
desktop = Desktop::all()->where('identifier',$identifier);
which was returning a record set, with multiple records. Even though there was only 1 record, it was treating it as a collection. So I tried changing
desktop = Desktop::all()->where('identifier',$identifier)->first();
which only fetched me a single record and it worked.

mockito when thenReturn does not return stubbed array list

List<Populate> fullAttrPopulateList = getFullAtrributesPopulateList(); //Prepare return list
when(mockEmployeeDao.getPopulateList(null)).thenReturn(fullAttrPopulateList);
MyDTO myDto = testablePopService.getMyPopData(); //Will call mockEmployeeDao.getPopulateList(null)
//verify(mockEmployeeDao,times(1)).getPopulateList(null);
assertEquals(fullAttrPopulateList.size(), myDto.getPopData().size()); //This fails because myDto.getPopData().size() returns 0
As you can see testablePopService.getMyPopData() calls mockEmployeeDao.getPopulateList(null) but when I debug it a zero sized list returns instead of the stubbed array list which is prepared by getFullAtrributesPopulateList();
If I uncomment the verify statement, it passes the test meaning getPopulateList(null) behavior does get called.
Can anyone give me some advice why my stubbed array list cannot be returned even it is verified the expected behavior happened? How come an empty array list returns rather than a null if I did something wrong?
First, check that the method is non-final and visible throughout its hierarchy. Mockito can have trouble mocking methods that are hidden (e.g. overriding a package-private abstract class's implementation); also, Mockito is entirely unable to mock final methods, or even to detect that the method is final and warn you about it.
You may also want to check that your call is using the overload you expect. Mockito's default list return value is an empty list, so you may simply stubbing one method and seeing the default value for another. You can see which method interactions Mockito has added by adding a call to verifyZeroInteractions temporarily, and Mockito will throw an exception with a list of calls that mock has received. You could also add verifyNoMoreInteractions, and even leave it in there, at the expense of test brittleness (i.e. the test breaks when the actual code continues to work).

How to correctly Deserialize obects while catching errors

I have a class DataFile which is the top level class that I am serializing. DataFile contains an ArrayCollection which in-turn contains objects which extend ArrayData, each which overrides readExternal in different ways.
Over the course of the development the ArrayData object from version 1.0 is now different than the ArrayData object in version 1.1.
This causes deserialization to fail, most often with a null object error.
This is expected behavior. What I would like to happen is that in cases where an error occurs the object would simply be ignored and we would continue deserialization.
To effect this I have thrown try..catch logic into the deserialization for the ArrayData
override public function readExternal(input:IDataInput):void {
try {
testvalue = input.readObject();
newTestValue = input.readObject(); //a value that is not a part of the file being deserialized
catch(e:Error){
//an error occurred, but just keep going
}
}
I was hoping that this would allow the deserialization chain to continue doing its thing and then would allow me to clean up the broken data after everything has been deserialized.
The problem I am now having is that the serialization of the ArrayCollection fails immediately after an error is caught in a bad ArrayData object with a index out of bounds error.
Error #2006: The supplied index is out
of bounds.
I have tried faking data in the catch portion of te ArrayData object but nothing works. I don't know why the error is bubbling up the deserialization chain and I do not know how to prevent this.
If anyone has any ideas on where I should go next in an attempt to resolve this issue I would appreciate the feedback.
Thanks,
Dan

Casting a retrieved Mediator with PureMVC as its proper class returns null

I have a mediator that I've registered for a navigation page:
facade.registerMediator(new NavPageMediator(viewComponent));
I'm trying to retrieve that mediator on another page like so:
var navPageMediator:NavPageMediator = facade.retrieveMediator(NavPageMediator.NAME) as NavPageMediator;
However, that statement returns null. If I try to cast it using the NavPageMediator(facade.retrieveMediator(NavPageMediator.NAME)) syntax instead, I get a TypeError: Error #1034: Type Coercion failed: cannot convert com.website.mvc.view.page::NavPageMediator#237560a1 to com.website.mvc.view.page.NavPageMediator.`
I can't, for the life of me, understand why NavPageMediator#237560a1 would be unable to convert to NavPageMediator, nor what happened in between registering the mediator and retrieving it that caused this. Especially since trace(new NavPageMediator() as NavPageMediator); returns [object NavPageMediator].
Incidentally, and this may be part of my problem, I don't understand what the #hash at the end of the object is (#237560a1). Is it simply an internal identifier for that class instance?
Edit:
Left a bit of important info: The SWF in which I instantiate and register the mediator is separate from the SWF in which I try to retrieve it.
Figured it out. It turned out to be an ApplicationDomain issue. Assigning both SWFs (the registrant and the retriever) to the same domain solved the issue.
Additionally, I'm pretty sure the #hash at the end of the class name is an internal reference to the ApplicationDomain to which the class belongs. So NavPageMediator#237560a1 was in a different domain than NavPageMediator (why there was no hash on the second one I'm still not sure; that would have made things a bit clearer).